473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2736
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
4323
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
2169
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 multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
20
10043
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 advance for enlightment ... here's the snippet #!/usr/bin/python
22
23324
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 examples?
47
3605
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4883
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%)...
15
28322
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 an error saying you are not allowed Multiple base classes. /// <summary>
7
3721
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 goes like this: Shape | +-- Ellipse | +-- Circle
47
3965
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple...
2
2548
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). -- Paul for cls in (A,B,C,D): seen = set()
0
7490
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...
0
7425
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...
0
7682
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. ...
0
7780
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...
1
5351
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
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...

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.