problem on inheritance

S

shuisheng

Dear All,

I have a question on inheritance. Assume I have a base class Vector and
its derived class DecoratedVector as follows:

class Vector
{
// Constructors.
Vector();
Vector(size_t num, double initValue);
Vector(const double *pData, size_t num);
... // Other several constructors.
};

class DecoratedVector : public Vector
{
...

private:
size_t id;
};

The DecoratedVector is in fact a Vector with a decorated id. What I am
wondering is that if there is any way to ease the work of writing the
constructors for class DecoratedVector, since they are always the same
as those in class Vectors.

Thank you for your kind help!

Shuisheng
 
V

Victor Bazarov

shuisheng said:
I have a question on inheritance. Assume I have a base class Vector
and its derived class DecoratedVector as follows:

class Vector
{
// Constructors.
Vector();
Vector(size_t num, double initValue);
Vector(const double *pData, size_t num);
... // Other several constructors.
};

class DecoratedVector : public Vector
{
...

private:
size_t id;
};

The DecoratedVector is in fact a Vector with a decorated id. What I am
wondering is that if there is any way to ease the work of writing the
constructors for class DecoratedVector, since they are always the
same as those in class Vectors.

No, there isn't. Constructors are not inherited, so you have to write
all constructor that will have the empty bodies and follow this pattern:

DecoratedVector(type arg) : Vector(arg) {}

V
 
K

Kai-Uwe Bux

shuisheng said:
Dear All,

I have a question on inheritance. Assume I have a base class Vector and
its derived class DecoratedVector as follows:

class Vector
{
// Constructors.
Vector();
Vector(size_t num, double initValue);
Vector(const double *pData, size_t num);
... // Other several constructors.
};

class DecoratedVector : public Vector
{
...

private:
size_t id;
};

The DecoratedVector is in fact a Vector with a decorated id. What I am
wondering is that if there is any way to ease the work of writing the
constructors for class DecoratedVector, since they are always the same
as those in class Vectors.

To some degree, you can use templated constructors:

class DecoratedVector : public Vector {
public:

DecoratedVector ( void ) :
Vector ()
{}

DecoratedVector ( DecoratedVector const & other ) :
Vector ( other )
{}

template < typename A >
DecoratedVector ( A a ) :
Vector ( a )
{}

template < typename A, typename B >
DecoratedVector ( A a, B b ) :
Vector ( a, b )
{}

template < typename A, typename B, typename C >
DecoratedVector ( A a, B b, C c ) :
Vector ( a, b, c )
{}

virtual ~DecoratedVector ( void ) {}

}; // DecoratedVector

This will forward all constructors with up to 3 arguments.


Best

Kai-Uwe Bux
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top