473,583 Members | 4,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual base classes and constructor calls

Hello

I recently had troubles with a class inheritance hierarchy.

I solved it, but it didn't satisfied me.

I found the solution using this forum :)

Actualy i found the following message (with no responces associated) :
----------------------------------

I ran into an interesting situation with respect to multiple
inheritance and virtual base classes. The following example
illustrates the problem:

class A {
A(int) { /*... */ ; }
};

class B : virtual public A {
B(int a, int b) : A(a) { /* ... */ ; }
};

class C : virtual public A {
C(int a, int c) : A(a) { /* ... */ ; }
};

class D : public B, public C {
D(int a, int b, int c, int d) : A(a), B(a,b), C(a,c) { /* ... */; }
} class E : public D {
#ifdef WIBNI /* [1] */
// what I would LIKE to be able to do
E(int a, int b, int c, int d, int e) : D(a,b,c,d) { /* ... */; }
#else
// what I actually HAVE to do
E(int a, int b, int c, int d, int e) : A(a),D(a,b,c,d) { /* ... */; }
#endif
}

/* [1] Wouldn't It Be Nice If */

The English explanation: My inheritance graph is as follows:

A
/ \
B C
\ /
D
|
E

"A" is a virtual base class of "B" and "C". "A" does not have a
default constructor (my application is a bit more involved than the
above).

"B" and "C" are base classes of "D". I understand and support that
"D"'s constructor(s) must specify how the virtual base class "A" is
constructed. (If this was not the case, there could be ambiguities in
how "A" was actually constructed, given that the calls to "A"'s
constructor from "B" and from "C" could be different.) This is
explained in the ARM S12.6.2, pp. 292--4.

What I don't understand is why "E" must also declare how the virtual
base class "A" is to be constructed - shouldn't the compiler be able
to figure out that the "virtualnes s" of "A" at "E" comes about at "D",
and thus do the right thing:
1) try to construct a "D", the base class of "E"
2) note that "A" is a multiply-inherited virtual base class of
"D" - and thus that "A"'s constructor must be called with
the arguments as specified by "D"'s constructor ("A"'s
default constructor if none is declared).
3) construct "A"
4) try to construct "B" - the first base class of "D"
5) note that "A" is a virtual base class of "B" - note that
"A" has already been constructed - "B"'s "A" is done -
ignore)
6) execute "B"'s constructor
7) try to construct "C" - the second base class of "D"
5) note that "A" is a virtual base class of "C" - note that
"A" has already been constructed - "C"'s "A" is done -
ignore)
6) execute "C"'s constructor
7) execute "D"'s constructor
8) execute "E"'s constructor

.......
------------------------------------------------
I truncated it because the rest of it wasn't relevent to my problem.

If you wish, you can find the original message with google forum
search engine using the following key words :
"virtual class bad constructor call"

My question is simple : what could have been answered to this post ?
I think there must be a reason why this haven't been done this way in
the C++ standard, but i can't figure why.

Would such a compiler be too slow ?

I wander.

Regards

Benoit Lefevre.
Jul 19 '05 #1
0 4244

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

Similar topics

18
2204
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
4603
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
3
3768
by: Marcin Kalicinski | last post by:
Hi, Having the following program: #include <iostream> struct A { A() { std::cout << "A::A()\n"; } A(const A &) { std::cout << "A::A(const A &)\n"; }
4
2340
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which template is created and I tried to define a virtual method, that's called from the base template ctor. Here's my example code: #include...
5
2616
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
6
616
by: Carl R. Davies | last post by:
I was reading this link http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198 heading "14.10 Virtual Constructors" I am struggling to understand the issue the author is trying to solve. This paragraph is confusing me: "As we have seen (section 14.2) C++ supports virtual destructors. Like many other object oriented languages...
1
3462
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } // A virtual destructor
17
3514
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
1
1776
by: donesbon | last post by:
Hi ... I'm faily new to C++ but have 6 years experience in other languages ... still C++ and the syntax puzzles me a great deal ... I have the following problem ... I have a base class with a constructor that calls an empty function ... what I want is for this empty function to be substituted by an overridden version in SOME of the derived...
0
7894
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8191
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...
0
6578
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...
1
5700
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5372
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
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...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1431
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.