0

Possible Duplicate:
Initializing in constructors, best practice?
Advantages of using initializer list?

I have the following two ways to define the constructor in the Point Class :

class Point
{
public : 
    Point(double X,double Y):x(X),y(Y){}

Private : 
double x,y;


}

Another way :

class Point
{
public : 
 Point(double X,double Y)
{    
   x= X;
   y = Y;

}

Private : 
double x,y;


}

I want to know which one is better and why?Is there is the use of copy ctor in the first case? Where each one is preferred?Can some explain with the example? Rgds, Softy

Community
  • 1
  • 1
Raulp
  • 7,758
  • 20
  • 93
  • 155

3 Answers3

3

Use initializer lists when possible. Although in this particular case it makes no difference, you'll get in the habit.

For POD types, the members don't get initialized twice so performance-wise it's the same thing. non-POD types are initialized before entering the constructor body, so they'll be initialized twice if you don't do it in the initializer list but in the body of the c-tor.

const members and references must be initialized in the initializer list. Again, doesn't apply to your case.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

The second version does an assignment to the data members, whereas the first initializes them to the given values. The first version is preferred here. Although it may make little difference in the case of doubles, there is no reason at all to prefer a construction that performs extra operations. If your data members were not doubles, but types that are expensive to construct, you would be paying the penalty of default constructing them, and then assigning a value to them.

Example:

struct ExpensiveToConstruct { .... };

struct Foo {
  Foo() {
    // here, x has already been default constructed
    x = SomeValue; // this is an assignment to the already constructed x.
  }
  ExpensiveToConstruct x;
};

struct Bar {
  Bar : x(SomeValue) {
    // only the constructor has been called. No assignemt.
  }
  ExpensiveToConstruct x;
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    "Although it may make little difference in the case of doubles" - does it make **any** difference? – Luchian Grigore Jun 03 '12 at 09:36
  • @LuchianGrigore there is an extra assignment, so I think it makes a (negligible) difference. – juanchopanza Jun 03 '12 at 09:38
  • But in the first case there is an extra initialization. – Luchian Grigore Jun 03 '12 at 09:39
  • See 6th paragraph - http://www.parashift.com/c%2B%2B-faq-lite/ctors.html#faq-10.6 – Luchian Grigore Jun 03 '12 at 09:40
  • @LuchianGrigore I cannot say for sure whether the two are guaranteed equivalent in terms of performance, and the article doesn't back that statement up. – juanchopanza Jun 03 '12 at 09:43
  • 1
    They aren't guaranteed equivalent, but I don't see any reason why they shouldn't be. After all, on the assembler level, there is no RAII: for a POD object, you first allocate a bunch of unititialised memory and then start to write stuff in it, regardless of whether this is an initialisation or an assignment in the C++. – leftaroundabout Jun 03 '12 at 11:08
  • @juanchopanza " but types that are expensive to construct, you would be paying the penalty of default constructing them, and then assigning a value to them." you mean if I have the user defined data type(struct) then it is good to use the first method? "paying the penalty of default constructing them," as in second method? Can you plz explain your answer in more detail like where I have to pay the penalty and where the initialization is happening twice and so forth?That would be great !! thanks !! – Raulp Jun 03 '12 at 18:23
  • @softy well, if the members are not POD types, when you use the second method, the members get default initialized first, then they have values assigned to them in the constructor's body. It is like doing `SomeType x; x = SomethingElse;`. – juanchopanza Jun 03 '12 at 18:42
  • @juanchopanza and the members get initialized in both the methods by default right !! its just that the doubly initializing them (POD members) in the second method will be heavy. – Raulp Jun 03 '12 at 18:50
  • @softy in my first example, they are default initialized, ant *then* have a given value *assigned* to them. In my second example, they are directly initialized to the given value, avoiding the assignment (which is typically expensive if construction is expensive). – juanchopanza Jun 03 '12 at 18:53
  • @softy and note that my second example corresponds to your first and vice versa :-) – juanchopanza Jun 03 '12 at 18:56
-1

Better to use initialize list in your ctor. It would be more efficient. In your 2nd way, ctor will initialize member data twice, with default value 1st, and then invoke statement in ctor.

and more, for const or reference member, should be initialized by init lists, cannot be initialized in ctor.

JsDoITao
  • 114
  • 4