1

Possible Duplicate:
What is the member variables list after the colon in a constructor good for?

I found this code, its a constructor for Vector3f class.

Vector3f::Vector3f()
    : x(0)
    , y(0)
    , z(0)
{

}

what does this definition ": x(0), y(0), z(0)" mean, and how would I go around using it?

Community
  • 1
  • 1
Chebz
  • 1,375
  • 3
  • 14
  • 27
  • If there isn't a FAQ entry for this, there should be. Looking... – Fred Larson Jul 15 '11 at 17:03
  • There is: http://stackoverflow.com/questions/210616/what-is-the-member-variables-list-after-the-colon-in-a-constructor-good-for – Fred Larson Jul 15 '11 at 17:05
  • @Fred to be fair, it doesn't really explain why you would want to use initializer lists, and half is spent on explaining the :: operator – Josh Jul 15 '11 at 17:08
  • @Josh: I see your point. Is [this](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) a better reference? – Fred Larson Jul 15 '11 at 17:10
  • @Fred Admitting I have some bias towards the answer I just gave, yeah, that is a pretty good reference :) – Josh Jul 15 '11 at 17:13

3 Answers3

4

This is called a class initilizer list. There must be 3 member variables, x y z and it is setting all of them to 0.

Check out http://www.cprogramming.com/tutorial/initialization-lists-c++.html

Class initialization lists are neat, because they allow you to set initial values of class state right before the code in the constructor is executed. This is different from just assigning values in your constructor, and makes a number of things possible:

  1. They allow you to assign values to const members
  2. They are the only way to pass values to the constructor of the parent class
  3. They are the only way to instantiate stack-based member objects of the class that require some kind of values passed to the constructor.
Josh
  • 6,155
  • 2
  • 22
  • 26
3

It is called Initialization list in C++.

In initializes your variables x, y and z.

In this case(assuming x, y & z are int) it is same as:

x = 0
y = 0
z = 0

As @Charles Bailey, appropriately points out, In case the types are not int but some custom user defined class then assignment & construction maynot be the exact equivalents.

Explanation:
In an Initializer list, the types are Initialized by calling appropriate default constructors on each of the variable, for an inbuilt data type like int this is same as assignment but for custom classes an constructor operation might be different from assignment operation.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    You can't really say that it's the same in this case without knowing the types of `x`, `y` and `z`. Certainly, if they were (e.g.) `int`s then it would have the same effect, but they might have class type with different behaviour for construction and assignment or they might even be `const`. – CB Bailey Jul 15 '11 at 16:54
  • @Charles Bailey: That's correct, I just assumed them to be `integer`, but indeed it should be clarified. I added the detail to answer. – Alok Save Jul 15 '11 at 18:21
1

For primitive types it's the same as

x = 0;
y = 0;
z = 0;

Though actually there's subtle difference between initializer list and assignment inside a body. When we initialize fields via initializer list the constructors will be called once. If we use the assignment then fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

Mike Mozhaev
  • 2,367
  • 14
  • 13