Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI, I have a question but cannot find the answer for a long time, that is, what's difference between the following 2 statements about the parameter initialization?

C++
class A::A() 
    : a(0), b(0), c(0) 
{ 
}

class A::A() 
{ 
    a = 0 
    b = 0; 
    c = 0; 
} 

I know there is "direct initialization" and "copy initialization", but I don't know what's the other differences and if there is any description about the first statement?

Thanks in advance
Posted
Updated 13-Nov-13 22:47pm
v2

If a, b and c are of a type other than a built-in type (like int or bool) then using the initialization list will give you a performance increase as the object is created "in place".

In general, initialization is preferred over assignment. There are exceptions you this rule but I think Mr Cline can cover them better[^] than I can.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
CPallini 15-Nov-13 3:22am    
5.
In the first case the objects a, b, and c are initialized by calling their respective constructors. In the second, they are assigned a value via the assignment operator.

As long as your objects are simple int or float variables, there is practically no difference between these two forms of initialization. However, if they are are complex class objects, the difference might be huge! For classes without a default constructor you are even forced to use the initializer list; the compiler enforces that.

But even with classes that have a default constructor, the difference between initializing a fresh object and assigning a new value to an existing object can be huge.
 
Share this answer
 
The difference between the 2 statements is that in the first statement the parameters are initialized by the order of the members in the class definition and not in the order of the parameter list. With a parameter list you can also initialize a reference which you can't do in the body.
 
Share this answer
 
v2
There are two differences:

1. member initialization - in the first case copy constructors will be called for each member, in the second case, default constructors will be called. In case of POD types (plain old data, like int, char, double, float), the latter don't exist, of course, and the former are equivalent to simple memory initialization.

2. Number of steps to take - the second form will call assignment operators in addition to memory initialization as described in 1. Of course for POD types, there's nothing to do in step 1, so you don't actually perform more steps than for the first form.

There may be a third difference depending on your compiler and possibly on whether you compile in debug or release mode: Some compilers like to initialize all allocated memory before it's being used: either with 0 to create consistent starting values, or with some hex pattern like 0xA5 that they can use to diagnose access to uninitialized memory. If this is true, then the first form will always be faster, because by providing initialization values you prevent the compiler from performing this default inialization.

So to sum it up, if you have members of simple types, then there is really no or little difference for both forms. If some of these members are of class type however, the first form is always better, as it requires fewer operations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900