initializer list question

S

spipyeah

My compiler is known not to be very compliant. So I can't base myself
on the fact that the following doesn't work to determine whether or
not it is legal C++.

In an initializer list, can you pass objects as parameters (by
reference or otherwise) to other objects constructed later in the
initializer list?

Example:


class Obj
{
public:
Obj( int a ) {};
};

class ObjConsumer
{
public:
ObjConsumer( Obj &first_obj, Obj &second_obj ) {};
};

class myclass
{
public:
myclass();

protected:
Obj obj1, obj2;
ObjConsumer consumer;
};

myclass::myclass() :
obj1(5),
obj2(7),
consumer( obj1, obj2 )
{
}
 
K

Karl Heinz Buchegger

spipyeah said:
My compiler is known not to be very compliant. So I can't base myself
on the fact that the following doesn't work to determine whether or
not it is legal C++.

In an initializer list, can you pass objects as parameters (by
reference or otherwise) to other objects constructed later in the
initializer list?

The tricky point is that the order in which you put things in the
initializer list doesn't affect the order in which things get constructed.

The base class will always be constructed first.
Then the member objects of that class get constructed in the
order in which they appear in the class declaration:
class myclass
{
public:
myclass();

protected:
Obj obj1, obj2; // obj1 will be constructed first
// obj2 will be constructed second
ObjConsumer consumer; // consumer will be constructed third
};

myclass::myclass() :
obj1(5),
obj2(7),
consumer( obj1, obj2 )

You could turn this around

consumer( obj1, obj2 ),
obj2( 7 ),
obj1( 5 )

yet the construction and thus the initialization sequence will be:

obj1, obj2, consumer
 
G

Gary Labowitz

spipyeah said:
My compiler is known not to be very compliant. So I can't base myself
on the fact that the following doesn't work to determine whether or
not it is legal C++.

In an initializer list, can you pass objects as parameters (by
reference or otherwise) to other objects constructed later in the
initializer list?

Example:


class Obj
{
public:
Obj( int a ) {};
};

class ObjConsumer
{
public:
ObjConsumer( Obj &first_obj, Obj &second_obj ) {};
};

class myclass
{
public:
myclass();

protected:
Obj obj1, obj2;
ObjConsumer consumer;
};

myclass::myclass() :
obj1(5),
obj2(7),
consumer( obj1, obj2 )
{
}
Did you try this? It works fine. Of course, it doesn't DO anything.
 
S

spipyeah

You could turn this around
consumer( obj1, obj2 ),
obj2( 7 ),
obj1( 5 )

yet the construction and thus the initialization sequence will be:

obj1, obj2, consumer

Thank you, that was very helpful!

Alban
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top