473,406 Members | 2,620 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 base class and construction/destruction order

In case of inheritence the order of execution of constructors is in
the order of derivation and order of destructor execution is in
reverse order of derivation.

Is this case also true in case class is derived as virtual?

How does the order of construction/destruction is impacted if the base
class is derived as virtual or non virtual? just see the example
below.

I am using VS6/SP6. what the standard say on this.
#include <iostream>
using namespace std;

class A
{
int val;
public:
~A(){cout <<"~A()\n";}
A() { cout <<"A()\n"; }
};

class B{
int val;
public:
~B(){cout <<"~B()\n";}
B(){cout <<"B()\n";}
};

class C
{
int val;
public:
~C(){cout <<"~C()\n";}
C(){cout <<"C()\n";}
};

class F
{
int val;
public:
~F(){cout <<"~F()\n";}
F(){cout <<"F()\n";}
};

class D: public A, public B, public C, public F
{
int val;
public:
~D(){cout <<"~D()\n";}
D(){cout <<"D()\n";}

};

class K: public A, virtual public B, public C, public F
{
int val;
public:
~K(){cout <<"~K()\n";}
K(){cout <<"K()\n";}
};
class L: public A, virtual public B, virtual public C, public F
{
int val;
public:
~L(){cout <<"~L()\n";}
L(){cout <<"L()\n";}
};

int main()
{
{
cout << "For D\n\n";
D obj;
}

{
cout << "For K\n\n";
K obj;
}

{
cout << "For L\n\n";
L obj;
}

return 0;
}
/*
Output is:
For D

A()
B()
C()
F()
D()
~D()
~F()
~C()
~B()
~A()

For K

B()
A()
C()
F()
K()
~K()
~F()
~C()
~A()
~B()

For L

B()
C()
A()
F()
L()
~L()
~F()
~A()
~C()
~B()

*/

Feb 6 '07 #1
7 3388
* BeautifulMind:
In case of inheritence the order of execution of constructors is in
the order of derivation and order of destructor execution is in
reverse order of derivation.

Is this case also true in case class is derived as virtual?
Yes.

The only problem is that the virtual base class' constructor arguments
must be specified (explicitly, or for a default constructor implicitly)
by the most derived class.

That's because there's only a single call of that constructor, and the
various specifications in not-most-derived-classes could easily conflict.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 6 '07 #2
Hi

BeautifulMind wrote:
How does the order of construction/destruction is impacted if the base
class is derived as virtual or non virtual? just see the example
below.

I am using VS6/SP6. what the standard say on this.
It says that
a) virtual base classes are initialized in the most derived class
b) virtual base classes are initialized first
c) the order is determined by the order of appearance in a depth-first
search through the inheritance graph.

For example,

A : virtual B, C
B : D, virtual E
C : virtual F, G

corresponds to the following graph:
A
/ \
/ \
B C
/ \ / \
D E F G

Let's see... When doing the depth-first search, we find the virtual base
classes in the following order:
E, B, F
So E will be initialized first. Next is B, but this requires prior
initialization of D. So the order of initialization will be
E, D, B, F, G, C, A
As usual, the order is reversed at destruction.

Markus
Feb 6 '07 #3
BeautifulMind wrote:
In case of inheritence the order of execution of constructors is in
the order of derivation and order of destructor execution is in
reverse order of derivation.

Is this case also true in case class is derived as virtual?

How does the order of construction/destruction is impacted if the base
class is derived as virtual or non virtual? just see the example
below.
The destruction is the reverse of construction.

The order of construction is well defined and is paraphrased here:

1. First all the virtual base classes are constructed.

Then starting at the most derived class, and running recursively

2. The classes direct non-virtual bases are constructed
3. The non-static class members are constructed
4. The constructor body is executed.
Feb 6 '07 #4
Markus Moll wrote:
It says that
a) virtual base classes are initialized in the most derived class
b) virtual base classes are initialized first
c) the order is determined by the order of appearance in a depth-first
search through the inheritance graph.

For example,

A : virtual B, C
B : D, virtual E
C : virtual F, G

corresponds to the following graph:
A
/ \
B C
/ \ / \
D E F G

Let's see... When doing the depth-first search, we find the virtual base
classes in the following order:
E, B, F
So E will be initialized first. Next is B, but this requires prior
initialization of D. So the order of initialization will be
E, D, B, F, G, C, A
As usual, the order is reversed at destruction.

For a class, the order of the member initialization list has to match
the order of declarations within the class. Does this rule apply when
we pass arguments to base class constructors?

what is the correct order of including the the following in member
initilization list (if this order really matter):
1. non constant and const data members of a class
2. virtual base classes
3. non-virtual base class

Regards,
BeautifulMind

Feb 7 '07 #5
Reporting for clarity:-

For a class, the order of the member initialization list should match
the order of declarations within the class. Does this rule apply when
we pass arguments to base class constructors in member initialization
list?

What is the correct order of including the following in member
initialization list (if this order really matter):

1. Non-constant and const data members of a class
2. Virtual base class constructors
3. Non-virtual base class constructors

Regards,
BeautifulMind

Feb 7 '07 #6
On Feb 6, 5:02 pm, Markus Moll <markus.m...@esat.kuleuven.bewrote:
Hi

BeautifulMindwrote:
How does the order of construction/destruction is impacted if the base
class is derived as virtual or non virtual? just see the example
below.
I am using VS6/SP6. what the standard say on this.

It says that
a) virtual base classes are initialized in the most derived class
b) virtual base classes are initialized first
c) the order is determined by the order of appearance in a depth-first
search through the inheritance graph.

For example,

A : virtual B, C
B : D, virtual E
C : virtual F, G

corresponds to the following graph:
A
/ \
/ \
B C
/ \ / \
D E F G

Let's see... When doing the depth-first search, we find the virtual base
classes in the following order:
E, B, F
So E will be initialized first. Next is B, but this requires prior
initialization of D. So the order of initialization will be
E, D, B, F, G, C, A
As usual, the order is reversed at destruction.

Markus

Thanks for the nice explanation Markus

Feb 8 '07 #7
BeautifulMind wrote:
>
For a class, the order of the member initialization list has to match
the order of declarations within the class. Does this rule apply when
we pass arguments to base class constructors?
Nothing changes the order of initialization from what I stated.
Not the passing of arguments, not the presence of member initializers.
>
what is the correct order of including the the following in member
initilization list (if this order really matter):
1. non constant and const data members of a class
2. virtual base classes
3. non-virtual base class
As I said before, the virtual base classes are initialized before
all others.

Then starting at the most derived class:
The direct bases (non-virtual) are initialized
The non-static members
Then the constructor body runs.

Const makes no difference to initialization or destruction.
Feb 8 '07 #8

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

Similar topics

7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
11
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
4
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;
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
17
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;"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
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
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...
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
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.