Code: Select all
Constructor():
pointer1 ( new class1()),
int1 (10)
{
}
Code: Select all
Constructor()
{
pointer1 = new class1();
int1 = 10;
}
Code: Select all
Constructor():
pointer1 ( new class1()),
int1 (10)
{
}
Code: Select all
Constructor()
{
pointer1 = new class1();
int1 = 10;
}
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.Initializing using an initialization list is always legal, never less efficient than an assignment in the constructor body, and often more efficient.