redeclaring (pure) virtual functions in derived classes?

S

Steven T. Hatton

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
 
J

John Carson

Steven T. Hatton said:
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?
 
S

Steven T. Hatton

John said:
Steven T. Hatton said:
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
 
J

John Carson

Steven T. Hatton said:
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()
{
}
 
D

David White

Steven T. Hatton said:
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
 
S

Steven T. Hatton

John said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top