Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this weird colon-member (" : ") syntax in the constructor?

Recently I've seen an example like the following:

#include <iostream>  class Foo { public:   int bar;   Foo(int num): bar(num) {}; };  int main(void) {   std::cout << Foo(42).bar << std::endl;   return 0; } 

What does this strange : bar(num) mean? It somehow seems to initialize the member variable but I've never seen this syntax before. It looks like a function/constructor call but for an int? Makes no sense for me. Perhaps someone could enlighten me. And, by the way, are there any other esoteric language features like this, you'll never find in an ordinary C++ book?

like image 871
nils Avatar asked Nov 10 '09 23:11

nils


People also ask

What is colon in constructor?

It's called an initialization list. It initializes members before the body of the constructor executes.

What is the syntax for constructor?

Syntax of constructor functionsThe declaration integer v1; initializes our object v1 and assigns the data members a and b the value 2 . With normal member functions, we have to write a statement to initialize each of the objects.

What is the use of colon in C++?

In C++, the colon ":" operator, to my understanding, is used for inheritance.

What is constructor give its syntax and example?

The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method. Example e = Example(0, 50); // Explicit call. Example e2(0, 50); // Implicit call.


2 Answers

Foo(int num): bar(num)     

This construct is called a Member Initializer List in C++.

Simply said, it initializes your member bar to a value num.


What is the difference between Initializing and Assignment inside a constructor?

Member Initialization:

Foo(int num): bar(num) {}; 

Member Assignment:

Foo(int num) {    bar = num; } 

There is a significant difference between Initializing a member using Member initializer list and assigning it an value inside the constructor body.

When you initialize fields via Member initializer list the constructors will be called once and the object will be constructed and initialized in one operation.

If you use assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.

Cost of Member Initialization = Object Construction  Cost of Member Assignment = Object Construction + Assignment 

The latter is actually equivalent to:

Foo(int num) : bar() {bar = num;} 

While the former is equivalent to just:

Foo(int num): bar(num){} 

For an inbuilt (your code example) or POD class members there is no practical overhead.


When do you HAVE TO use Member Initializer list?

You will have(rather forced) to use a Member Initializer list if:

  • Your class has a reference member
  • Your class has a non static const member or
  • Your class member doesn't have a default constructor or
  • For initialization of base class members or
  • When constructor’s parameter name is same as data member(this is not really a MUST)

A code example:

class MyClass { public:   // Reference member, has to be Initialized in Member Initializer List   int &i;   int b;   // Non static const member, must be Initialized in Member Initializer List   const int k;    // Constructor’s parameter name b is same as class data member   // Other way is to use this->b to refer to data member   MyClass(int a, int b, int c) : i(a), b(b), k(c) {     // Without Member Initializer     // this->b = b;   } };  class MyClass2 : public MyClass { public:   int p;   int q;   MyClass2(int x, int y, int z, int l, int m) : MyClass(x, y, z), p(l), q(m) {} };  int main() {   int x = 10;   int y = 20;   int z = 30;   MyClass obj(x, y, z);    int l = 40;   int m = 50;   MyClass2 obj2(x, y, z, l, m);    return 0; } 
  • MyClass2 doesn't have a default constructor so it has to be initialized through member initializer list.
  • Base class MyClass does not have a default constructor, So to initialize its member one will need to use Member Initializer List.

Important points to Note while using Member Initializer Lists:

Class Member variables are always initialized in the order in which they are declared in the class.

They are not initialized in the order in which they are specified in the Member Initializer List.
In short, Member initialization list does not determine the order of initialization.

Given the above it is always a good practice to maintain the same order of members for Member initialization as the order in which they are declared in the class definition. This is because compilers do not warn if the two orders are different but a relatively new user might confuse member Initializer list as the order of initialization and write some code dependent on that.

like image 112
Alok Save Avatar answered Oct 12 '22 10:10

Alok Save


It's a member initialization list. You should find information about it in any good C++ book.

You should, in most cases, initialize all member objects in the member initialization list (however, do note the exceptions listed at the end of the FAQ entry).

The takeaway point from the FAQ entry is that,

All other things being equal, your code will run faster if you use initialization lists rather than assignment.

like image 30
James McNellis Avatar answered Oct 12 '22 10:10

James McNellis