473,406 Members | 2,705 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,406 software developers and data experts.

virtual inheritance question

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;
}

Sep 4 '06 #1
4 2594
ch******@gmail.com 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.
Sep 4 '06 #2

John Grabner wrote:
ch******@gmail.com 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

Sep 4 '06 #3
pa**********@att.net 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<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

Sep 4 '06 #4
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?

Sep 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: cyrusNew | last post by:
Hi, sorry if it was already asked, I have a following class diagram A =? A | | B C \ /
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.