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()
*/ 7 3399
* 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?
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
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.
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
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
On Feb 6, 5:02 pm, Markus Moll <markus.m...@es at.kuleuven.bew rote:
Hi
BeautifulMindwr ote:
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 disadvantages of doing so
/ or not as the case maybe?
2) In the following example, are code sections BD and DD *both* going to get
called? I assume they both always will, but is this guaranteed beyond all
odds in all cases? (When I say is it...
|
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;
|
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 the case when the class contains at
least one virtual member function? Shouldn't the destructor always be
virtual, whenever there's a possibility that an inherited object will
be destructed through a base class pointer? (This does not require,
|
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 a VIRTUAL function
createList(). This createList() function is overloaded in another class
named ct_server that inherits gs_object.
in my code, it looks something like that :
| |
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 exception
neutral/
I got a reply from Bux with his code for a smart pointer with far fewer
lines of code
and more cleaner design, not over engineered like mine.
...
|
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;
|
by: Dennis Jones |
last post by:
Hello,
I have a couple of classes that look something like this:
class RecordBase
{
};
class RecordDerived : public RecordBase
{
|
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
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
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
| |