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

Order of c'tor call in case of virtual inheritance

Hi,

The ISO-C++ spec says that the order of construction for C++ objects
is:

************************************************** **************
....
Initialization shall proceed in the following order:

* First, and only for the constructor of the most derived class as
described below, virtual base classes shall be initialized in the order
they appear on a depth-first left-to-right traversal of the directed
acyclic graph of base classes, where ``left-to-right'' is the order of
appearance of the base class names in the derived class
base-specifier-list.

* Then, direct base classes shall be initialized in declaration
order as they appear in the base-specifier-list (regardless of the
order of the mem-initializers).

.....

************************************************** ***********

Can someone clarify the algorithm with a non-trivial example ?

ashani

Nov 26 '05 #1
3 1649
as*******@gmail.com wrote:
Hi,

The ISO-C++ spec says that the order of construction for C++ objects
is: ....
Can someone clarify the algorithm with a non-trivial example ?


See if you understand the output of this code.

#include <iostream>
#include <ostream>

int x = 0;

template <char ch>
struct C
{
C()
{
std::cout << ch << "()";
}

C( int z )
{
std::cout << ch << "(" << z << ")";
}

~C()
{
std::cout << "~" << ch << "()";
}
};

struct A
: C<'a'>, virtual C<'b'>, virtual C<'c'>, C<'d'>, C<'e'>
{
C<'f'> v3;

A()
: C<'c'>( 100 )
{
}
};

struct B
: virtual C<'g'>, C<'h'>, A, virtual C<'b'>, C<'i'>, C<'e'>
{
C<'j'> v5;
C<'k'> v6;

B()
: C<'c'>(x++), C<'i'>(x++), v6(x++), v5(x++), C<'h'>(x++),
C<'b'>(x++)
{
}
};
int main()
{
B b;
}
Nov 26 '05 #2
On Sat, 26 Nov 2005 11:28:30 -0800, Gianni Mariani
<gi*******@mariani.ws> wrote:
as*******@gmail.com wrote:
Hi,

The ISO-C++ spec says that the order of construction for C++ objects
is:...

Can someone clarify the algorithm with a non-trivial example ?


See if you understand the output of this code.


Very clever demo! Especially the fact that C<'c'>(100) is never called
will surprise many people who have never seen anything like this.
#include <iostream>
#include <ostream>

int x = 0;

template <char ch>
struct C
{
C()
{
std::cout << ch << "()";
}

C( int z )
{
std::cout << ch << "(" << z << ")";
}

~C()
{
std::cout << "~" << ch << "()";
}
};

struct A
: C<'a'>, virtual C<'b'>, virtual C<'c'>, C<'d'>, C<'e'>
{
C<'f'> v3;

A()
: C<'c'>( 100 )
{
}
};

struct B
: virtual C<'g'>, C<'h'>, A, virtual C<'b'>, C<'i'>, C<'e'>
{
C<'j'> v5;
C<'k'> v6;

B()
: C<'c'>(x++), C<'i'>(x++), v6(x++), v5(x++), C<'h'>(x++),
C<'b'>(x++)
{
}
};
int main()
{
B b;
}


--
Bob Hairgrove
No**********@Home.com
Nov 26 '05 #3
Hi,
Thanks for the example.
This is a marvellous example !!! It is also a very good test case.
With this test case and creating some more (by modifying the order,
making A virtual, etc)
my understading of the algorithm is as follows: (correct me if I am
mistaken):

1. Draw the DAG for the inheritance diagram. Mark each type of edge
(whether inheritance is virtual or nonvirtual).
2. For each node (which is a class) number the edges 1, 2, ... in the
order in which they appear in the declaration.
3. Start off with the bottom-most class (i.e. the object to be
contructed)
4. Now perform LRV like visit and construct all classes which are
virtual base classes immediately as you visit each node. Do not
contruct the same class twice.
5. After all virtual base classes have been constructed revisit from
starting (as we did in 3). Now construct objects for the remaining
non-virtual base classes.

Is the above algorithm correct?
What was confusing me was the "depth-first" keyword. Basically do you
construct objects as you visit each node when performing depth-first
traversal OR you first construct the leaf nodes and as you backtrack
you construct the rest, as my understanding goes?

ashani

Nov 27 '05 #4

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

Similar topics

5
by: Mark | last post by:
When declaring a class that uses multiple inheritance, does the order used when listing the inheritance matter? I'm finding with my compiler (gcc 3.2.2) that my program seg faults when destructing...
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...
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; };
7
by: BeautifulMind | last post by:
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...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack...
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?
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
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,...
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
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.