473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem on inheritance

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

--
N0 comp.lang.c++, NO my c++ life.

Sep 27 '06 #1
2 1629
shuisheng wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '06 #2
shuisheng wrote:
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
Sep 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: Morten Aune Lyrstad | last post by:
Hi again! I'm having problems with inheritance. I have a base interface class called IObject. Next I have two other interfaces classes, IControl and ICommandMaster, which derives from IObject. ...
0
by: Bita-kookoo | last post by:
I am having a problem with inheritance for my web solution. Here are the steps I took for a project within C#: -I first created a new class, descended from System.Web.UI.Page. For this...
1
by: Galileo | last post by:
I want to ask some questions about ASP, but not ASP. NET, sorry for my cross post because i don't know where asp newsgroup is First, is it possible to use user defined class in ASP, if so, how?...
0
by: Mariano | last post by:
Hi, I have posted a bug of the forms designer that overwrites always the Icon, thus preventing Icon inheritance when you derive from a form. I am trying to overcome this by adding an ImageList in...
9
by: surendran.d | last post by:
hi, can diamond inhertance problem be solved using virtual functions,, or can only be done with scope resolution operators.. is there any other way to solve this problem... Thanks, suri
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.