2

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

I am looking at this. What does the part after the colon mean ?

explicit Box(double l, double w, double h) : Rectangle(l, w), height(h) {}

I am used to initializing the values in the function body {}.

Community
  • 1
  • 1
Sudhir
  • 325
  • 1
  • 3
  • 7
  • You cannot "declare values in the body". What you're saying shows that your understanding of the language is in its very early stages, and once you get to understand variable declaration and initialization, you will naturally come to ask how to initialize class members, and thence to the colon in the constructor. Check out our FAQ for good book recommendations! – Kerrek SB Nov 09 '11 at 12:21
  • I meant initializing in the body. – Sudhir Nov 09 '11 at 12:32
  • Same problem; that does not exist, either. – Kerrek SB Nov 09 '11 at 12:34

4 Answers4

2

It is used for initialization of the members and parent classes of your class or struct.

You could do it for member variables inside the function body… except for const members!

Also see this section of the C++ FAQ.

Benoit
  • 76,634
  • 23
  • 210
  • 236
1

Box is a sub-class of Rectangle, and its constructor first calls the Rectangle constructor and then sets the height variable to h

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Part of it is a call to the base class constructor, part is a member initializer list.

The explicit keyword is not needed there, since implicit conversions can only happen for conversion constructors.

Because members and base classes are initialized before entering the code in your constructor, this provides an alternative to the initialization. There's no point in doing it twice, right? Besides the speed gain, a call to the base class constructor inside your constructor's body is not possible, so it will always call the default constructor. If you wish to call Rectangle(l, w), you must do it before entering the constructor code: Box(double l, double w, double h) : Rectangle(l, w).

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

This is the actual way of initializing the data members. Normally constructors have two phases, namely initialization and computation.

/* Initialization */
explicit Box(double l, double w, double h): Rectangle(l, w), height(h)

And whatever you do inside { }, that comes under computation phase.

Even if you don't write a initialize list in your code the compiler puts hidden code to initialize your data members. So if you write

explicit Box(double l, double w, double h):Rectangle(l, w)  
{  
  height = h; /* re-assigning value */ 
}

It means you are just assigning values to which are all ready been initialized.

explicit Box(double l, double w, double h): Rectangle(l, w), height(h)

This type of initialization is mandatory when you have constant or reference data members in your class. Because you can't assign values to them inside the constructor body i.e. inside { }

paper.plane
  • 1,201
  • 10
  • 17
  • Note that without an explicit initializer, the members are *not* initialized if they are PODs (esp. built-in types). Especially for pointer members you cannot assume that they are NULL unless you explicitly initialized them to NULL. – celtschk Nov 09 '11 at 12:53