On Tue, 23 Sep 2003 10:12:41 +0200, Gert Van den Eynde
<> 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