473,326 Members | 2,173 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,326 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 2707
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.