Initialization list question

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Initialization list question

Post by Neo »

Is the following code

Code: Select all

Constructor():
pointer1 ( new class1()),
int1 (10)
{
}
the same as the below code (the way it works at least)

Code: Select all

Constructor()
{
  pointer1 = new class1();
  int1 = 10;
}
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Initialization list question

Post by Solar »

No.

Aside from the first code part being potentially more efficient, the second would fail if pointer1 or int1 were const. (The former would not fail in this case.)

Second post will follow as soon as I find that "Effective C++" book again. ;)
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Initialization list question

Post by Solar »

Ah, there it is... ;-)

Once the constructor body is entered, all member variables of the class have been initialized - by values in the initialization list where available, or by their default constructor otherwise.

That is the reason why you cannot assign new values to const or reference types: They have been initialized, and their very nature forbids reassignment.

As for the claim for efficiency, consider a [tt]string[/tt] member. If you assign it a value within the constructor body, you not only have a call to the string() default constructor, but another one to string.operator=() in the constructor body. A sufficiently stupid C++ compiler / library might actually do three calls: string(), string( char * ), and string.operator=()...

Meyers writes on this:
Initializing using an initialization list is always legal, never less efficient than an assignment in the constructor body, and often more efficient.
One exception is when you have lots of native datatypes as members - let's say, two dozen [tt]int[/tt] members. Here, there's no costly "default constructor" to be called; and writing an initialization list for two dozen [tt]int[/tt]s for every constructor (don't forget the various copy / cast constructors) would be awkard, and error-prone. In this case, write a private (inline) [tt]init()[/tt] function that does the initialization, and call it from each constructor.
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Initialization list question

Post by Neo »

Ok I wanted the code to the above (initilization list code) but was not sure if new could also be used there.
@Solar: Thanks for the detailed info.
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Initialization list question

Post by Solar »

No problem, you're welcome.

And phrased like that, yes, if you can write the second, you can also write the first. It's the other way round that makes troubles. ;)
Every good solution is obvious once you've found it.
Post Reply