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?
It's called an initialization list. It initializes members before the body of the constructor executes.
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.
In C++, the colon ":" operator, to my understanding, is used for inheritance.
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.
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
.
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.
You will have(rather forced) to use a Member Initializer list if:
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.MyClass
does not have a default constructor, So to initialize its member one will need to use Member Initializer List.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With