Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Abstract base class + Functor

Reply
Thread Tools

Abstract base class + Functor

 
 
Gert Van den Eynde
Guest
Posts: n/a
 
      09-23-2003
Hi all,

I'm struggling a bit with Functors generated for an ABC.

This is the functor code:

class Functor{
public:
virtual double operator(double x)=0
}

template <class T> class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
}
public:
SpecFunctor(T* _obj, double(T::*_fpt)(double x))
{
obj = _obj; fpt = _fpt;
}

virtual operator()(double x)
{
(*obj.*fpt)(x);
}

Now, this works fine for 'ordinary' classes T. However, I would like to have
it working for an ABC. When I use an ABC for T, I get warnings from g++
that obj_ will get initialzed after... I more or less understand why the
warnings appear (you cannot create an object for an ABC).

It's merely warnings, but I would like to clean it up. Is there a way to do
this without giving up the Functor or ABC?

Thanks for any tips,

gert

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      09-23-2003
On Tue, 23 Sep 2003 10:12:41 +0200, Gert Van den Eynde
<(E-Mail Removed)> wrote:

>Hi all,
>
>I'm struggling a bit with Functors generated for an ABC.
>
>This is the functor code:
>
>class Functor{
>public:
>virtual double operator(double x)=0
>}
>
>template <class T> class SpecFunctor: public Functor
>{
>private:
>double (T::*fpt)(double x);
>T* obj;
>}
>public:
>SpecFunctor(T* _obj, double(T::*_fpt)(double x))
>{
>obj = _obj; fpt = _fpt;
>}
>
>virtual operator()(double x)
>{
>(*obj.*fpt)(x);
>}
>
>Now, this works fine for 'ordinary' classes T. However, I would like to have
>it working for an ABC. When I use an ABC for T, I get warnings from g++
>that obj_ will get initialzed after... I more or less understand why the
>warnings appear (you cannot create an object for an ABC).


You don't show any code that tries to create an object of abstract
type.

>
>It's merely warnings, but I would like to clean it up. Is there a way to do
>this without giving up the Functor or ABC?


Once I'd fixed the syntax errors, added virtual destructors, etc., it
worked fine:

class Functor{
public:
virtual ~Functor(){}
virtual double operator()(double x) = 0;
};

template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
bj(obj), fpt(fpt)
{
}

virtual double operator()(double x)
{
return (*obj.*fpt)(x);
}
};

struct ABClass
{
~ABClass(){}
virtual double f(double d) = 0;
};

struct Derived: ABClass
{
virtual double f(double d)
{
return d * d;
}
};

#include <iostream>

int main()
{
Derived d;
Functor* f = new SpecFunctor<ABClass>(&d, &ABClass::f);
std::cout << (*f)(10) << '\n';
}

Or was that not what you meant?

Tom
 
Reply With Quote
 
 
 
 
Gert Van den Eynde
Guest
Posts: n/a
 
      09-23-2003
>
> Or was that not what you meant?
>


Yes, indeed. But compiling your code with g++ -Wall gives the same warnings:


test.cc:26: warning: `struct ABClass' has virtual functions but non-virtual
destructor
test.cc:32: warning: `struct Derived' has virtual functions but non-virtual
destructor
test.cc: In constructor `SpecFunctor<T>::SpecFunctor(T*, double
(T::*)(double))
[with T = ABClass]':
test.cc:44: instantiated from here
test.cc:12: warning: `SpecFunctor<ABClass>:bj' will be initialized after
test.cc:11: warning: `double (ABClass::*SpecFunctor<ABClass>::fpt)(double

The code works, as did mine, I just want to get rid of the warnings... I
suppose that when the compiler generates them, there must be a cleaner
way,no?

gert

 
Reply With Quote
 
Attila Feher
Guest
Posts: n/a
 
      09-23-2003
Gert Van den Eynde wrote:
>> Or was that not what you meant?
>>

>
> Yes, indeed. But compiling your code with g++ -Wall gives the same
> warnings:
>
>
> test.cc:26: warning: `struct ABClass' has virtual functions but
> non-virtual destructor


struct ABClass
{
virtual ~ABClass(){}
virtual double f(double d) = 0;
};

TADA

--
Attila aka WW


 
Reply With Quote
 
Gert Van den Eynde
Guest
Posts: n/a
 
      09-23-2003
> struct ABClass
> {
> virtual ~ABClass(){}
> virtual double f(double d) = 0;
> };
>
> TADA


That one I figured out myself shouldn't have copy/pasted it... It's the
'will be initialized after' that I wonder about...

gert
 
Reply With Quote
 
David B. Held
Guest
Posts: n/a
 
      09-23-2003
"Gert Van den Eynde" <(E-Mail Removed)> wrote in message
news:bkp3hl$8va$(E-Mail Removed)...
> [...]
> That one I figured out myself shouldn't have copy/pasted it... It's
> the 'will be initialized after' that I wonder about...


template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
bj(obj), fpt(fpt)
{
}
// ...
};

The order of initialization is determined by the order of the data
members within the class, not the order of initializers in the c'tor.
Therefore, if you give a different order in the c'tor, the compiler
is warning you that it will not respect that ordering. To eliminate
the warning, make the initializer list match the data member
ordering.

Dave


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      09-23-2003
On Tue, 23 Sep 2003 11:25:07 +0200, Gert Van den Eynde
<(E-Mail Removed)> wrote:

>> struct ABClass
>> {
>> virtual ~ABClass(){}
>> virtual double f(double d) = 0;
>> };
>>
>> TADA

>
>That one I figured out myself shouldn't have copy/pasted it... It's the
>'will be initialized after' that I wonder about...


That's a good example of GCC's nanny warnings, where it annoys you by
complaining about non-errors. Change the functor to:


template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
:fpt(fpt), obj(obj) //re-ordered these
//to match the declaration order.
{
}

virtual double operator()(double x)
{
return (*obj.*fpt)(x);
}
};

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
abstract base class containing class scg_ C++ 6 02-03-2009 04:50 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Abstract class with no abstract functions Uzytkownik C++ 3 04-03-2005 05:45 PM



Advertisments