Order of c'tor call in case of virtual inheritance

A

ashaniray

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
 
G

Gianni Mariani

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;
}
 
B

Bob Hairgrove

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;
}
 
A

ashaniray

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top