Connecting Tech Pros Worldwide Forums | Help | Site Map

virtual inheritance question

chrolson@gmail.com
Guest
 
Posts: n/a
#1: Sep 4 '06
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;
}

John Grabner
Guest
 
Posts: n/a
#2: Sep 4 '06

re: virtual inheritance question


chrolson@gmail.com wrote:
Quote:
For the following classes, why is the output 1 instead of 10?:
....
Quote:
>
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.
pauldepstein@att.net
Guest
 
Posts: n/a
#3: Sep 4 '06

re: virtual inheritance question



John Grabner wrote:
Quote:
chrolson@gmail.com wrote:
Quote:
For the following classes, why is the output 1 instead of 10?:
...
>
Quote:

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

Todd Gardner
Guest
 
Posts: n/a
#4: Sep 4 '06

re: virtual inheritance question


pauldepstein@att.net wrote:
Quote:
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<Rightright(boost::shared_ptr<Bot tom>(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

chrolson@gmail.com
Guest
 
Posts: n/a
#5: Sep 8 '06

re: virtual inheritance question


The virtual base class constructor must be called
Quote:
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?

Closed Thread


Similar C / C++ bytes