473,651 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1666
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 ~DecoratedVecto r ( 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
1964
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 to the parent in the inheritance tree). 2. This hierarchy evolved over time to different versions for each class. So for example, version's 1 hierarchy would be A_v1 <-B_v1 <-..<-Z_v1. 3. At runtime, only one version is selected by a factory...
4
2890
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
3
2466
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. My problem is that I have a /third/ class, CCommand, which derives from both IControl and ICommandmaster... The error message says (Weird.....)
0
1114
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 description, let's call this class "MyBasePage". Every webform page within my solution will descend from this class instead of System.Web.UI.Page. The reason for this is so that I have a common base class from which I can put custom validation,...
1
1197
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? and does ASP support something like inheritance? I know vb only support something like interface, is it the same as ASP? Is there any example for ASP inheritance or interface?
0
978
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 the base class. However, this doesn't solve my problem about Icon inheritance. The idea is to put Icon showing code inside the base form class. I add this code to the new() sub: 'Show the inherited icon Me.Icon = Icon.FromHandle((New...
9
3294
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
3595
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 should not be. But if I compile with g++-4.0.3 the following snippet: class A { int i;
9
10963
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
2896
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: tlkpColor (PK) tlkpColorID
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.