Multiple Inheritance

C

colint

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
 
V

Victor Bazarov

colint said:
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
 
R

Rolf Magnus

colint said:
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.
 
M

mlimber

colint said:
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-lite/multiple-inheritance.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
 
C

colint

Rolf said:
Neither B nor C will have two instances of A.


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
 
B

BigBrian

colint said:
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
{

};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top