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 a ordinary C++ book?
This is called an initialization list. It is an alternate way of initializing class members. There are benefits to using this instead of simply assigning new values to the members in the body of the constructor, but if you have class members which are constants or references they must be initialized.
The other already explained to you that the syntax that you observe is called "constructor initializer list". This syntax lets you to custom-initialize base subobjects and member subobjects of the class (as opposed to allowing them to default-initialize or to remain uninitialized).
I just want to note that the syntax that, as you said, "looks like a constructor call", is not necessarily a constructor call. In C++ language the
()
syntax is just one standard form of initialization syntax. It is interpreted differently for different types. For class types with user-defined constructor it means one thing (it is indeed a constructor call), for class types without user-defined constructor it means another thing (so called value initialization ) for empty()
) and for non-class types it again means something different (since non-class types have no constructors).In your case the data member has type
int
.int
is not a class type, so it has no constructor. For typeint
this syntax means simply "initializebar
with the value ofnum
" and that's it. It is done just like that, directly, no constructors involved, since, once again,int
is not a class type of therefore it can't have any constructors.It's an initialization list for the constructor. Instead of default constructing
x
,y
andz
and then assigning them the values received in the parameters, those members will be initialized with those values right off the bat. This may not seem terribly useful forfloat
s, but it can be quite a timesaver with custom classes that are expensive to construct.This is not obscure, it's the C++ initialization list syntax
Basically, in your case,
x
will be initialized with_x
,y
with_y
,z
with_z
.This construct is called a Member Initializer List in C++.
Simply said, it initializes your member
bar
to a valuenum
.What is the difference between Initializing and Assignment inside a constructor?
Member Initialization:
Member Assignment:
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.
The latter is actually equivalent to:
While the former is equivalent to just:
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:
A code example:
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.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.
I don't know how you could miss this one, it's pretty basic. That's the syntax for initializing member variables or base class constructors. It works for plain old data types as well as class objects.