Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual inheritance question

Reply
Thread Tools

virtual inheritance question

 
 
chrolson@gmail.com
Guest
Posts: n/a
 
      09-04-2006
For the following classes, why is the output 1 instead of 10?:

class Top
{
public:
Top():a(1){}
Top(int arg):a(arg){}
virtual ~Top(){}
int a;
};

class Left : virtual public Top
{
public:
Left():b(2){}
int b;
};

class Right : virtual public Top
{
public:
Right(): c(3), Top(10){}
int c;
};

class Bottom : public Left, public Right
{
public:
Bottom():d(4){}
int d;
};

int main()
{
Bottom* bottom = new Bottom();
Right *right = bottom;
cout << "right->a: " << right->a << endl;

return 0;
}

 
Reply With Quote
 
 
 
 
John Grabner
Guest
Posts: n/a
 
      09-04-2006
wrote:
> For the following classes, why is the output 1 instead of 10?:

....

>
> class Right : virtual public Top
> {
> public:
> Right(): c(3), Top(10){}
> int c;
> };
>
> class Bottom : public Left, public Right
> {
> public:
> Bottom():d(4){}
> int d;
> };
>
> int main()
> {
> Bottom* bottom = new Bottom();
> Right *right = bottom;
> cout << "right->a: " << right->a << endl;
>
> return 0;
> }
>


The virtual base class constructor must be called
from the most derived constructor. In this case it must
be called from Bottom.

John.
 
Reply With Quote
 
 
 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      09-04-2006

John Grabner wrote:
> wrote:
> > For the following classes, why is the output 1 instead of 10?:

> ...
>
> >
> > class Right : virtual public Top
> > {
> > public:
> > Right(): c(3), Top(10){}
> > int c;
> > };
> >
> > class Bottom : public Left, public Right
> > {
> > public:
> > Bottom():d(4){}
> > int d;
> > };
> >
> > int main()
> > {
> > Bottom* bottom = new Bottom();
> > Right *right = bottom;
> > cout << "right->a: " << right->a << endl;
> >
> > return 0;
> > }
> >

>
> The virtual base class constructor must be called
> from the most derived constructor. In this case it must
> be called from Bottom.
>
> John.


I know this is a bit of a digression from the OP's question but
shouldn't there be a delete Bottom; to avoid a memory leak? (I'm not
completely sure -- this is a genuine question, not a rhetorical one.)

Paul Epstein

 
Reply With Quote
 
Todd Gardner
Guest
Posts: n/a
 
      09-04-2006
wrote:
> I know this is a bit of a digression from the OP's question but
> shouldn't there be a delete Bottom; to avoid a memory leak? (I'm not
> completely sure -- this is a genuine question, not a rhetorical one.)
>
> Paul Epstein


In my opinion, yes and no. Yes, the memory needs to be deleted, but no,
not by delete. Here would be my main:

int main()
{
boost::shared_ptr<Right> right(boost::shared_ptr<Bottom>(new
Bottom()));
cout << "right->a: " << right->a << endl;

return 0;
}

The shared_ptr cleans up for you when it goes out of scope.

Todd Gardner

 
Reply With Quote
 
chrolson@gmail.com
Guest
Posts: n/a
 
      09-08-2006
> The virtual base class constructor must be called
> from the most derived constructor. In this case it must
> be called from Bottom.
>
> John.


So what happens to the explicit call to Top(10) in the initializer list
of class Right? Does it get called at all?

 
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
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM
Question about virtual inheritance lok C++ 2 11-17-2003 03:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57