473,471 Members | 4,629 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

initializer list question

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 )
{
}
Jul 19 '05 #1
3 2891


spipyeah wrote:

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
{
}


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2
"spipyeah" <sp*******@yahoo.com> wrote in message
news:88**************************@posting.google.c om...
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.
--
Gary
Jul 19 '05 #3
> 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
Jul 19 '05 #4

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

Similar topics

1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
4
by: jfindlay | last post by:
Is it possible to populate a container with an initializer list? I am not too particular about how hackish or ugly any suggestions might be, I'm simply interested in investigating possible...
6
by: Marty M | last post by:
The following causes the "invalid initializer" message during gcc compile time... char firstword = word(question,1); the "word" function is... char * word(char *phrase, int what) {...body...
1
by: Vincent RICHOMME | last post by:
Hi, Let' say I have a class with an object pointer as member something like: // classA.h class A { A(B* pB); B* m_pB; };
2
by: cody | last post by:
class Test { IList list = new ArrayList(); MyCollection list2 = new MyCollection (list); } Leads to this error. I know I could initialize them in the ctor but I'm asking myself where this...
3
by: Ham Pastrami | last post by:
class Point { public: const int x, y; Point(int x, int y); } Point::Point(int x, int y) : x(x), y(y) { }
4
by: Ham Pastrami | last post by:
How do you initialize objects in the initializer list? Also, while this code is probably incorrect, it does compile (and execute) and I wonder what the actual result is. class Control { Point...
3
by: John Ratliff | last post by:
I've been using g++ for a long time (and this is not a g++ question), and I recently started using MSVC Express. Some of my programs that I rebuilt with MSVC give me a warning I don't understand. ...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
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
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
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,...
1
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,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.