473,394 Members | 1,781 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,394 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 2716
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.