redeclaring (pure) virtual functions in derived classes?

Discussion in 'C++' started by Steven T. Hatton, May 17, 2005.

  1. This is a question about coding styles. I've seen some cases where the
    programmer has redeclared the pure virtual functions from an interface
    class in the implementation derived from it. For example, I have this
    abstract base class:

    namespace widgets {
    template<Var_T=float>
    class VFunctor_IF {
    public:
    virtual void operator()(const std::vector<Var_T>& vars_ptr) = 0;
    };
    }

    I'm deriving this implementation from VFunctor_IF:
    //header file
    class RotationFunctor: public VFunctor_IF<float> {
    public:
    // To declare or not to declare?
    virtual void operator()(const std::vector<float>& vars_ptr);
    private:
    osg::ref_ptr<osg::positionAttitudeTransform*> _pat_ptr;
    };

    //source file
    void RotationFunctor::eek:perator()(const std::vector<Var_T>& vars) {
    using namespace osg;
    Matrix m = Matrix::rotate(vars[0], X_AXIS) * Matrix::rotate(vars[1],
    Y_AXIS) * Matrix::rotate(vars[2], Z_AXIS);

    Quat q;
    q.set(m);
    _pat_rptr->setAttitude(q);
    }

    Clearly, I have to implement the pure virtual VFunctor_IF::eek:perator() in the
    the derived class. My question is whether I should declare it in the class
    definition of RotationFunctor.

    Any opinions on this?
    --
    "If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true." - Bertrand
    Russell
     
    Steven T. Hatton, May 17, 2005
    #1
    1. Advertising

  2. Steven T. Hatton

    John Carson Guest

    "Steven T. Hatton" <> wrote in message
    news:
    > This is a question about coding styles. I've seen some cases where
    > the programmer has redeclared the pure virtual functions from an
    > interface class in the implementation derived from it. For example,
    > I have this abstract base class:
    >
    > namespace widgets {
    > template<Var_T=float>
    > class VFunctor_IF {
    > public:
    > virtual void operator()(const std::vector<Var_T>& vars_ptr) = 0;
    > };
    > }
    >
    > I'm deriving this implementation from VFunctor_IF:
    > //header file
    > class RotationFunctor: public VFunctor_IF<float> {
    > public:
    > // To declare or not to declare?
    > virtual void operator()(const std::vector<float>& vars_ptr);
    > private:
    > osg::ref_ptr<osg::positionAttitudeTransform*> _pat_ptr;
    > };
    >
    > //source file
    > void RotationFunctor::eek:perator()(const std::vector<Var_T>& vars) {
    > using namespace osg;
    > Matrix m = Matrix::rotate(vars[0], X_AXIS) *
    > Matrix::rotate(vars[1], Y_AXIS) * Matrix::rotate(vars[2], Z_AXIS);
    >
    > Quat q;
    > q.set(m);
    > _pat_rptr->setAttitude(q);
    > }
    >
    > Clearly, I have to implement the pure virtual VFunctor_IF::eek:perator()
    > in the the derived class. My question is whether I should declare it
    > in the class definition of RotationFunctor.
    >
    > Any opinions on this?



    Depends. Do you want it to compile?

    --
    John Carson
     
    John Carson, May 17, 2005
    #2
    1. Advertising

  3. John Carson wrote:

    > "Steven T. Hatton" <> wrote in message
    > news:
    >> This is a question about coding styles. I've seen some cases where
    >> the programmer has redeclared the pure virtual functions from an
    >> interface class in the implementation derived from it. For example,
    >> I have this abstract base class:
    >>
    >> namespace widgets {
    >> template<Var_T=float>
    >> class VFunctor_IF {
    >> public:
    >> virtual void operator()(const std::vector<Var_T>& vars_ptr) = 0;
    >> };
    >> }
    >>
    >> I'm deriving this implementation from VFunctor_IF:
    >> //header file
    >> class RotationFunctor: public VFunctor_IF<float> {
    >> public:
    >> // To declare or not to declare?
    >> virtual void operator()(const std::vector<float>& vars_ptr);
    >> private:
    >> osg::ref_ptr<osg::positionAttitudeTransform*> _pat_ptr;
    >> };
    >>
    >> //source file
    >> void RotationFunctor::eek:perator()(const std::vector<Var_T>& vars) {
    >> using namespace osg;
    >> Matrix m = Matrix::rotate(vars[0], X_AXIS) *
    >> Matrix::rotate(vars[1], Y_AXIS) * Matrix::rotate(vars[2], Z_AXIS);
    >>
    >> Quat q;
    >> q.set(m);
    >> _pat_rptr->setAttitude(q);
    >> }
    >>
    >> Clearly, I have to implement the pure virtual VFunctor_IF::eek:perator()
    >> in the the derived class. My question is whether I should declare it
    >> in the class definition of RotationFunctor.
    >>
    >> Any opinions on this?

    >
    >
    > Depends. Do you want it to compile?
    >

    You got me. I guess if it's pure virtual it has to be declared in the class
    definition. With a previously defined virtual function, that is not the
    case. But I'm not sure of the consequences of omitting it.
    --
    "If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true." - Bertrand
    Russell
     
    Steven T. Hatton, May 17, 2005
    #3
  4. Steven T. Hatton

    John Carson Guest

    "Steven T. Hatton" <> wrote in message
    news:
    >>

    > You got me. I guess if it's pure virtual it has to be declared in
    > the class definition. With a previously defined virtual function,
    > that is not the case.


    Does this compile for you? It doesn't for me.

    #include <iostream>
    using namespace std;

    struct Base
    {
    virtual void operator()(int x)
    {
    cout << "base operator\n";
    }
    };

    struct Derived : public Base
    {};

    void Derived::eek:perator()(int x)
    {
    cout << "derived operator\n";
    }

    int main()
    {
    }


    --
    John Carson
     
    John Carson, May 17, 2005
    #4
  5. Steven T. Hatton

    David White Guest

    "Steven T. Hatton" <> wrote in message
    news:...
    > John Carson wrote:
    > > Depends. Do you want it to compile?
    > >

    > You got me. I guess if it's pure virtual it has to be declared in the

    class
    > definition. With a previously defined virtual function, that is not the
    > case. But I'm not sure of the consequences of omitting it.


    Maybe I've missed your point completely, but any member function that you
    want to implement for a given class has to be declared in that class's class
    definition. There are no special rules for pure virtuals, plain virtuals or
    non-virtuals, except that where you have a pure virtual declaration in the
    class definition an implementation of the function is optional.

    DW
     
    David White, May 17, 2005
    #5
  6. John Carson wrote:

    > "Steven T. Hatton" <> wrote in message
    > news:
    >>>

    >> You got me. I guess if it's pure virtual it has to be declared in
    >> the class definition. With a previously defined virtual function,
    >> that is not the case.

    >
    > Does this compile for you? It doesn't for me.
    >
    > #include <iostream>
    > using namespace std;
    >
    > struct Base
    > {
    > virtual void operator()(int x)
    > {
    > cout << "base operator\n";
    > }
    > };
    >
    > struct Derived : public Base
    > {};
    >
    > void Derived::eek:perator()(int x)
    > {
    > cout << "derived operator\n";
    > }
    >
    > int main()
    > {
    > }
    >
    >

    I stand corrected again.
    --
    "If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true." - Bertrand
    Russell
     
    Steven T. Hatton, May 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    2
    Views:
    518
    Gianni Mariani
    Aug 27, 2003
  2. jeffc
    Replies:
    0
    Views:
    370
    jeffc
    Aug 27, 2003
  3. Replies:
    3
    Views:
    261
    Carl Banks
    Dec 17, 2006
  4. John Goche
    Replies:
    10
    Views:
    797
    Marcus Kwok
    Dec 8, 2006
  5. Francois Grieu
    Replies:
    5
    Views:
    568
    Alf P. Steinbach /Usenet
    Aug 4, 2010
Loading...

Share This Page