473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2612
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**********@at t.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_p tr<Rightright(b oost::shared_pt r<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

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
3010
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 out that you can inherit using the virtual keyword: class A { } class B : public *virtual* A
4
4650
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 class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
4
2901
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
3
1213
by: cyrusNew | last post by:
Hi, sorry if it was already asked, I have a following class diagram A =? A | | B C \ /
14
1930
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
1716
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 suppose, we're creating a little strategy game. In our game, there are Units. A Unit can be either a Human, or a Vehicle. Obviously, Human and Vehicle are subclasses of Unit.
2
1870
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 scenario... I have 5 Derived Classes I have 3 Base Classes
12
2665
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 up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
5
1991
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, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
23
4616
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
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.