By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

redeclaring (pure) virtual functions in derived classes?

P: n/a
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::operator()(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::operator() 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

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:y8********************@speakeasy.net
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::operator()(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::operator()
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
Jul 23 '05 #2

P: n/a
John Carson wrote:
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:y8********************@speakeasy.net
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::operator()(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::operator()
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

Jul 23 '05 #3

P: n/a
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:A7********************@speakeasy.net

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::operator()(int x)
{
cout << "derived operator\n";
}

int main()
{
}
--
John Carson
Jul 23 '05 #4

P: n/a
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:A7********************@speakeasy.net...
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

Jul 23 '05 #5

P: n/a
John Carson wrote:
"Steven T. Hatton" <ch********@germania.sup> wrote in message
news:A7********************@speakeasy.net

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::operator()(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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.