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 )
{
} 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
"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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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;
};
|
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...
|
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)
{
}
|
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...
|
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.
...
|
by: Pallav singh |
last post by:
How can we justify that initializer list is better in performance
than assignment list in constructor of C++ ??
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |