Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiple Inheritance

Reply
Thread Tools

Multiple Inheritance

 
 
colint
Guest
Posts: n/a
 
      07-20-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2006
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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-20-2006
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.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      07-20-2006
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

 
Reply With Quote
 
colint
Guest
Posts: n/a
 
      07-20-2006

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

 
Reply With Quote
 
BigBrian
Guest
Posts: n/a
 
      07-20-2006

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
{

};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments