473,386 Members | 1,908 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,386 software developers and data experts.

Multiple Inheritance

Hi
I'm fairly new to c++ and I have a question regarding inheritance. I'm
trying to create a class based on 2 inherited classes, e.g.

class A
{
...
}

class B: public A
{
...
}

class C: public B
{
...
}

The problem I'm having is how to ensure I have only 1 instance of class
A, as at the moment I have 2 calls to the constructor/destructor of A
for 1 instance of C. Will virtual inheritance solve this? I have tried
class A
{
...
}

class B: virtual public A
{
...
}

class C: public B
{
...
}
without success.
Thanks

Jul 20 '06 #1
5 2711
colint wrote:
I'm fairly new to c++ and I have a question regarding inheritance. I'm
trying to create a class based on 2 inherited classes, e.g.

class A
{
..
}
;
>
class B: public A
{
..
}
;
>
class C: public B
{
..
}
;
>
The problem I'm having is how to ensure I have only 1 instance of
class A, as at the moment I have 2 calls to the
constructor/destructor of A for 1 instance of C.
REALLY? How do you know that? I don't see any c-tor/d-tor in A or
in C.
Will virtual
inheritance solve this? I have tried class A
{
..
}

class B: virtual public A
{
..
}

class C: public B
{
..
}
without success.
No, virtual inheritance has nothing to do with it. Most likely
your understanding is flawed, probably in the "1 instance of C"
portion.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #2
colint wrote:
Hi
I'm fairly new to c++ and I have a question regarding inheritance. I'm
trying to create a class based on 2 inherited classes, e.g.

class A
{
..
}

class B: public A
{
..
}

class C: public B
{
..
}

The problem I'm having is how to ensure I have only 1 instance of class
A, as at the moment I have 2 calls to the constructor/destructor of A
for 1 instance of C.
You shouldn't have a problem in the above code. I suspect you omitted
something.
Will virtual inheritance solve this?
I have tried
class A
{
..
}

class B: virtual public A
{
..
}

class C: public B
{
..
}
See this FAQ and the one following on virtual inheritance:

http://www.parashift.com/c++-faq-lit....html#faq-25.8

But avoid inheritance if you don't need it. Multiple inheritance can
often (but certainly not always) indicate design problems. You should
use the weakest relationship between two classes that you can.
Composition is a weak relationship; inheritance is a strong
relationship.

Cheers! --M

Jul 20 '06 #3
colint wrote:
Hi
I'm fairly new to c++ and I have a question regarding inheritance. I'm
trying to create a class based on 2 inherited classes, e.g.

class A
{
..
}

class B: public A
{
..
}

class C: public B
{
..
}

The problem I'm having is how to ensure I have only 1 instance of class
A,
Neither B nor C will have two instances of A.
as at the moment I have 2 calls to the constructor/destructor of A
for 1 instance of C.
I doubt that. Show how you found that out.

Jul 20 '06 #4

Rolf Magnus wrote:
colint wrote:
Hi
I'm fairly new to c++ and I have a question regarding inheritance. I'm
trying to create a class based on 2 inherited classes, e.g.

class A
{
..
}

class B: public A
{
..
}

class C: public B
{
..
}

The problem I'm having is how to ensure I have only 1 instance of class
A,

Neither B nor C will have two instances of A.
as at the moment I have 2 calls to the constructor/destructor of A
for 1 instance of C.

I doubt that. Show how you found that out.

Ok, I just assumed it was to do with the multiple inheritance. Must be
something else, thanks

Jul 20 '06 #5

colint wrote:
>Ok, I just assumed it was to do with the multiple inheritance. Must be
something else, thanks
What multiple inheritance? There is no multiple inheritance in your
sample code. Multiple inheritance would be like this...

class C : public A, public B
{

};

Jul 20 '06 #6

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

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
15
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.