Abstract base class + Functor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gert  Van den Eynde

    Abstract base class + Functor

    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)(doubl e x);
    T* obj;
    }
    public:
    SpecFunctor(T* _obj, double(T::*_fpt )(double x))
    {
    obj = _obj; fpt = _fpt;
    }

    virtual operator()(doub le 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

  • tom_usenet

    #2
    Re: Abstract base class + Functor

    On Tue, 23 Sep 2003 10:12:41 +0200, Gert Van den Eynde
    <gvdeynde@hotma il.com> wrote:
    [color=blue]
    >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)(doubl e x);
    >T* obj;
    >}
    >public:
    >SpecFunctor( T* _obj, double(T::*_fpt )(double x))
    >{
    >obj = _obj; fpt = _fpt;
    >}
    >
    >virtual operator()(doub le 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).[/color]

    You don't show any code that tries to create an object of abstract
    type.
    [color=blue]
    >
    >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?[/color]

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

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

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

    virtual double operator()(doub le 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<ABC lass>(&d, &ABClass::f) ;
    std::cout << (*f)(10) << '\n';
    }

    Or was that not what you meant?

    Tom

    Comment

    • Gert  Van den Eynde

      #3
      Re: Abstract base class + Functor

      >[color=blue]
      > Or was that not what you meant?
      >[/color]

      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<AB Class>::obj' will be initialized after
      test.cc:11: warning: `double (ABClass::*Spec Functor<ABClass >::fpt)(doubl e

      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

      Comment

      • Attila Feher

        #4
        Re: Abstract base class + Functor

        Gert Van den Eynde wrote:[color=blue][color=green]
        >> Or was that not what you meant?
        >>[/color]
        >
        > 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[/color]

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

        TADA :-)

        --
        Attila aka WW


        Comment

        • Gert  Van den Eynde

          #5
          Re: Abstract base class + Functor

          > struct ABClass[color=blue]
          > {
          > virtual ~ABClass(){}
          > virtual double f(double d) = 0;
          > };
          >
          > TADA :-)[/color]

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

          gert

          Comment

          • David B. Held

            #6
            Re: Abstract base class + Functor

            "Gert Van den Eynde" <gvdeynde@hotma il.com> wrote in message
            news:bkp3hl$8va $1@naxos.belnet .be...[color=blue]
            > [...]
            > That one I figured out myself ;-) shouldn't have copy/pasted it... It's
            > the 'will be initialized after' that I wonder about...[/color]

            template <class T>
            class SpecFunctor: public Functor
            {
            private:
            double (T::*fpt)(doubl e x);
            T* obj;
            public:
            SpecFunctor(T* obj, double(T::*fpt) (double x))
            :obj(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


            Comment

            • tom_usenet

              #7
              Re: Abstract base class + Functor

              On Tue, 23 Sep 2003 11:25:07 +0200, Gert Van den Eynde
              <gvdeynde@hotma il.com> wrote:
              [color=blue][color=green]
              >> struct ABClass
              >> {
              >> virtual ~ABClass(){}
              >> virtual double f(double d) = 0;
              >> };
              >>
              >> TADA :-)[/color]
              >
              >That one I figured out myself ;-) shouldn't have copy/pasted it... It's the
              >'will be initialized after' that I wonder about...[/color]

              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)(doubl e 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()(doub le x)
              {
              return (*obj.*fpt)(x);
              }
              };

              Tom

              Comment

              Working...