I'm new to C++, and the whole idea of classes - I'm still reading a book to try and learn. The book I'm reading says that when I construct a class, I can assign default values by doing this:
class foo {
public:
foo(char c, int i);
private:
char exampleChar;
int exampleInt;
};
foo::foo(char c, int i):
exampleChar(c),
exampleInt(i)
{}
This code (to me) looks very messy, and doesn't follow rules that I'm used to in other languages. My question is, what's the difference between doing the above, and this (below, which I personally think looks a lot cleaner)?
foo::foo(char c, int i) {
exampleChar = c;
exampleInt = i;
}
Sort of things I'm thinking about are: are there performance/efficiency issues if done on a large scale - or is it exactly the same?
The first way, by doing : exampleChar(c), exampleInt(i) is called an initializer list.
If you do it the second way, the two variables are default constructed first1, then you assign them a value. (When the actual body of the constructor is entered, anything that hasn't been initialized by the initializer list is default constructed.) This is a waste of time because you're just overwriting the values anyway. For small types like int or char this isn't a big deal, but when those member variables are large types that would take lots of cycles to construct, you definitely want to use the initializer list.
The second way won't waste time giving them a default value and then overwriting it - it will set their values directly to that value you give it (or call the right constructor if the member is an object).
You can see what we mean by doing this:
class MyClass {
public:
int _i; // our data
// default constructor
MyClass() : _i(0) { cout << "default constructor"; }
// constructor that takes an int
MyClass(int i) : _i(i) { cout << "int constructor"; }
// assignment operator
void operator=(int i) { _i = i; cout << "assignment operator"; }
};
class OtherClass {
public:
MyClass c;
OtherClass() {
c = 54;
}
};
OtherClass oc;
You'll see that
default constructor
assignment operator
is printed. That's two function calls which, for other classes, could be expensive.
If you change the constructor of OtherClass to
OtherClass() : c(54) { }
You'll see that
int constructor
is printed. Just one call compared to two. This is the most efficient way.
Initializer lists are also a must when you
have types that have no default constructor. You have to call the right constructor in the initializer list.
have a const member that you want to give some value (rather than just have permantently the default value
have a reference member. You must use initializer lists on these.
tl;dr: do it because it's at least as fast but never slower than the other way, and sometimes far faster.
1 For built in types like int and char, they are actually not constructed at all; they just have the value of whatever memory they happen to have had previously.
The difference is that the compiler will always initialize all members (in declaration order) prior to the first user-defined constructor statement. In the case of a char and an int, which are both primitive types, 'initialization' actually means 'no initialization' here. However, if you have a member with a constructor that does some actual work, this constructor will be called upfront - if you do
foo::foo() {
myComplexMember = MyComplexClass(42);
}
the compiler did already invoke the MyComplexClass default constructor before your code got called, which is a waste of resources (and a compiler error if the default ctor is not accessible).
By using an initialization list, you can customize the default initialization and avoid doing things for nothing. Obviously, this is the way to go.
There are things you can do like that that you couldn't otherwise.
If one of the members doesn't have a default constructor. That's the only way you could initiate the member at construction. (same goes to base class)
You can assign a value to a const member.
You can assure a defined state for the class before the constructor function starts running.
If a member is a reference it needs to be initialized in the Initialization List. Because references are immutable and can be initialized only once in the beginning (like const)
Well, this is a typical FAQ question: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
In your case, using char and int there are no differences.
General rule: use initialization list as possibile, there are very few cases when you can prefer assignment, for example for improve readability:
MyClass::MyClass
{
a = b = c = d = e = f = 0;
}
is better than
class MyClass::MyClass : a(0), b(0), c(0), d(0), e(0), f(0) { }
If the members had non-trivial constructors, in the code below first the default constructors would be called, then the assignments would be executed, while in the code above they would be initialized only one time. So yes, there may be a performance issue.
There is also a practical issue: if they are const, references, or don't have default constructors, you can't use the version below.
There is a subtle, but important difference between those two options. What you have at the top is called a member initialization list. When the object is created, the members in this list are initialized to whatever you put in the parenthesis.
When you do assignment in the constructor body, the values are being first initialized, and then assigned. I'll post a short example below.
Example 1: Member initialization
class foo
{
public:
foo(char c, int i);
private:
char exampleChar;
int exampleInt;
Bar exampleBar;
};
foo::foo(char c, int i):
exampleChar(c),
exampleInt(i),
exampleBar() //Here, a bar is being default constructed
{
}
Example 2: Constructor Assignment
class foo
{
public:
foo(char c, int i, Bar b);
private:
char exampleChar;
int exampleInt;
Bar exampleBar;
};
foo::foo(char c, int i, Bar b):
//exampleChar(c),
//exampleInt(i),
//exampleBar()
{
exampleChar = c;
exampleInt = i;
exampleBar = someOtherBar; //Here, a bar is being assigned
}
This is where it gets interesting. Note that exampleBar is being assigned. Behind the scenes, a Bar is actually first being default constructed, even though you did not specify that. Furthermore, if your Bar is more complicated then a simple struct, you will need to be sure to implement the assignment operator in order for you to initialize it in this way. Even furthermore, in order to initialize the Bar from another Bar from the member initialization list, you must implement the copy constructor!
Example 3: Copy constructor used in member init
class foo
{
public:
foo(char c, int i, Bar b);
private:
char exampleChar;
int exampleInt;
Bar exampleBar;
};
foo::foo(char c, int i, Bar b):
//exampleChar(c),
//exampleInt(i),
exampleBar(b) //Here, a bar is being constructed using the copy constructor of Bar
{
exampleChar = c;
exampleInt = i;
}
I would get into the habit of using initialisation lists. They will not suffer from problems when somebody changes a char to some object where the default constructor is called first, and also for const correctness for the const values!
foo::foo(char c, int i):exampleChar(c),exampleInt(i){}
This construct is called a Member Initializer List in C++.
It initializes your member exampleChar to a value c & exampleInt to i.
What is the difference between Initializing And Assignment inside constructor? &
What is the advantage?
There is a difference between Initializing a member using initializer list and assigning it an value inside the constructor body.
When you initialize fields via initializer list the constructors will be called once.
If you use the 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.
For an integer data type(for which you use it) or POD class members there is no practical overhead.
Related
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?
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.
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.
That's constructor initialisation. It is the correct way to initialise members in a class constructor, as it prevents the default constructor being invoked.
Consider these two examples:
// Example 1
Foo(Bar b)
{
bar = b;
}
// Example 2
Foo(Bar b)
: bar(b)
{
}
In example 1:
Bar bar; // default constructor
bar = b; // assignment
In example 2:
Bar bar(b) // copy constructor
It's all about efficiency.
This is called an initialization list. It is a 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.
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.
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 type int this syntax means simply "initialize bar with the value of num" 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.
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.
This is an initialization list. It'll initialize the members before the constructor body is run.
Consider
class Foo {
public:
string str;
Foo(string &p)
{
str = p;
};
};
vs
class Foo {
public:
string str;
Foo(string &p): str(p) {};
};
In the first example, str will be initialized by its no-argument constructor
string();
before the body of the Foo constructor. Inside the foo constructor, the
string& operator=( const string& s );
will be called on 'str' as you do str = p;
Wheras in the second example, str will be initialized directly by
calling its constructor
string( const string& s );
with 'p' as an argument.
You are correct, this is indeed a way to initialize member variables. I'm not sure that there's much benefit to this, other than clearly expressing that it's an initialization. Having a "bar=num" inside the code could get moved around, deleted, or misinterpreted much more easily.
there is another 'benefit'
if the member variable type does not support null initialization or if its a reference (which cannot be null initialized) then you have no choice but to supply an initialization list
It's an initialization list for the constructor. Instead of default constructing x, y and z 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 for floats, but it can be quite a timesaver with custom classes that are expensive to construct.
Not mentioned yet on this thread: since C++11, the member initializer list can use list-initialization (aka. "uniform initialization", "braced initialization"):
Foo(int num): bar{num} {}
which has the same semantics as list-initialization in other contexts.
Although this is an old discussion, I couldn't find any mention about delegating constructor, which uses the weird ":" symbol in the following way.
class Foo
{
public:
Foo(char x, int y)
{}
Foo(int y) : Foo('a', y)
{}
};
What it does is simply delegating Foo(y) into Foo('a', y) . So that
Foo foo(15); //=> foo('a', 15)
When defining a delegating constructor, you cannot have any members in initializer list besides targeted constructor.
What is the colon syntax (:) in the class constructor and what does passing an integer to a std::vector<> constructor do?
I'd like to explain the below example from this duplicate question:
class UnionFind {
public:
UnionFind(int sz) : root(sz) {
for (int i = 0; i < sz; i++) {
root[i] = i;
}
}
private:
vector<int> root;
};
int main() {
UnionFind uf(10);
}
The colon (:) signifies the start of an "initialization list", or "initializer list", which initializes each variable to the value in parenthesis. It is as though you were calling a constructor for each variable with that value in parenthesis being passed in to that variable's constructor.
So, : root(sz) initializes the root variable with an int sz, which is like doing vector<int> root(sz);. Doing it like this, however, allows sz to get passed in to the UnionFind class constructor.
Initializing a vector with a size like that is constructor #3 here (https://en.cppreference.com/w/cpp/container/vector/vector):
// Constructor (3) as shown at
// https://en.cppreference.com/w/cpp/container/vector/vector
explicit vector( size_type count,
const T& value = T(),
const Allocator& alloc = Allocator());
It constructs count (sz in the example above) number of elements into the vector, each with value T(), which means int() in this case since root is a vector<int>. int() looks like a function call but is basically an integer default constructor to value zero (0). It is called "value initialization". Think of it like calling an "integer constructor", if integers were objects and had constructors. See also my question here: What is a call to char() as a function in C++?
So, let's recap: : root(sz) in the constructor is like constructing vector<int> root(sz);, which creates sz number of elements in the root vector, each with initial value int(), which is the syntax for "value initialization" of an int to zero.
Note also that the count parameter passed to constructor #3 shown above really should be a size_type, which can be written as std::vector::size_type, and is usually size_t (see the "Member types" section here). So, it would be better to change int sz to size_t sz instead. Therefore, change this constructor line: UnionFind(int sz) : root(sz) { to this instead: UnionFind(size_t sz) : root(sz) {.
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?
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.
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.
That's constructor initialisation. It is the correct way to initialise members in a class constructor, as it prevents the default constructor being invoked.
Consider these two examples:
// Example 1
Foo(Bar b)
{
bar = b;
}
// Example 2
Foo(Bar b)
: bar(b)
{
}
In example 1:
Bar bar; // default constructor
bar = b; // assignment
In example 2:
Bar bar(b) // copy constructor
It's all about efficiency.
This is called an initialization list. It is a 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.
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.
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 type int this syntax means simply "initialize bar with the value of num" 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.
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.
This is an initialization list. It'll initialize the members before the constructor body is run.
Consider
class Foo {
public:
string str;
Foo(string &p)
{
str = p;
};
};
vs
class Foo {
public:
string str;
Foo(string &p): str(p) {};
};
In the first example, str will be initialized by its no-argument constructor
string();
before the body of the Foo constructor. Inside the foo constructor, the
string& operator=( const string& s );
will be called on 'str' as you do str = p;
Wheras in the second example, str will be initialized directly by
calling its constructor
string( const string& s );
with 'p' as an argument.
You are correct, this is indeed a way to initialize member variables. I'm not sure that there's much benefit to this, other than clearly expressing that it's an initialization. Having a "bar=num" inside the code could get moved around, deleted, or misinterpreted much more easily.
there is another 'benefit'
if the member variable type does not support null initialization or if its a reference (which cannot be null initialized) then you have no choice but to supply an initialization list
It's an initialization list for the constructor. Instead of default constructing x, y and z 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 for floats, but it can be quite a timesaver with custom classes that are expensive to construct.
Not mentioned yet on this thread: since C++11, the member initializer list can use list-initialization (aka. "uniform initialization", "braced initialization"):
Foo(int num): bar{num} {}
which has the same semantics as list-initialization in other contexts.
Although this is an old discussion, I couldn't find any mention about delegating constructor, which uses the weird ":" symbol in the following way.
class Foo
{
public:
Foo(char x, int y)
{}
Foo(int y) : Foo('a', y)
{}
};
What it does is simply delegating Foo(y) into Foo('a', y) . So that
Foo foo(15); //=> foo('a', 15)
When defining a delegating constructor, you cannot have any members in initializer list besides targeted constructor.
What is the colon syntax (:) in the class constructor and what does passing an integer to a std::vector<> constructor do?
I'd like to explain the below example from this duplicate question:
class UnionFind {
public:
UnionFind(int sz) : root(sz) {
for (int i = 0; i < sz; i++) {
root[i] = i;
}
}
private:
vector<int> root;
};
int main() {
UnionFind uf(10);
}
The colon (:) signifies the start of an "initialization list", or "initializer list", which initializes each variable to the value in parenthesis. It is as though you were calling a constructor for each variable with that value in parenthesis being passed in to that variable's constructor.
So, : root(sz) initializes the root variable with an int sz, which is like doing vector<int> root(sz);. Doing it like this, however, allows sz to get passed in to the UnionFind class constructor.
Initializing a vector with a size like that is constructor #3 here (https://en.cppreference.com/w/cpp/container/vector/vector):
// Constructor (3) as shown at
// https://en.cppreference.com/w/cpp/container/vector/vector
explicit vector( size_type count,
const T& value = T(),
const Allocator& alloc = Allocator());
It constructs count (sz in the example above) number of elements into the vector, each with value T(), which means int() in this case since root is a vector<int>. int() looks like a function call but is basically an integer default constructor to value zero (0). It is called "value initialization". Think of it like calling an "integer constructor", if integers were objects and had constructors. See also my question here: What is a call to char() as a function in C++?
So, let's recap: : root(sz) in the constructor is like constructing vector<int> root(sz);, which creates sz number of elements in the root vector, each with initial value int(), which is the syntax for "value initialization" of an int to zero.
Note also that the count parameter passed to constructor #3 shown above really should be a size_type, which can be written as std::vector::size_type, and is usually size_t (see the "Member types" section here). So, it would be better to change int sz to size_t sz instead. Therefore, change this constructor line: UnionFind(int sz) : root(sz) { to this instead: UnionFind(size_t sz) : root(sz) {.
Using the c++11 standard, I can initialize class members in two ways:
class MyClass
{
private:
int a = 5;
};
or
class MyClass
{
private:
int a;
public:
MyClass()
{
a = 5;
}
};
Is either method superior to the other for any reason, or is it more an individual style choice?
The second example is not initialisation.
So, of the two examples, the first is the best way to initialise class members.
The traditional way to initialise looks like this:
class MyClass
{
private:
int a;
public:
MyClass()
: a(5)
{}
};
Though we now have inline initialisers as in your first example, since C++11.
This page from the Standard C++ Foundation suggests that, all else being equal, you should prefer to use an initializer list. That is:
Prefer
class Foo {
public:
Foo(int bar) : m_bar(bar) { }
private:
int m_bar;
};
over
class Foo {
public:
Foo(int bar) {
m_bar = bar;
}
private:
int m_bar;
};
Their rationale:
Consider the following constructor that initializes member object x_
using an initialization list: Fred::Fred() : x_(whatever) { }. The
most common benefit of doing this is improved performance. For
example, if the expression whatever is the same type as member
variable x_, the result of the whatever expression is constructed
directly inside x_ — the compiler does not make a separate copy of the
object. Even if the types are not the same, the compiler is usually
able to do a better job with initialization lists than with
assignments.
The other (inefficient) way to build constructors is via assignment,
such as: Fred::Fred() { x_ = whatever; }. In this case the expression
whatever causes a separate, temporary object to be created, and this
temporary object is passed into the x_ object’s assignment operator.
Then that temporary object is destructed at the ;. That’s inefficient.
As if that wasn’t bad enough, there’s another source of inefficiency
when using assignment in a constructor: the member object will get
fully constructed by its default constructor, and this might, for
example, allocate some default amount of memory or open some default
file. All this work could be for naught if the whatever expression
and/or assignment operator causes the object to close that file and/or
release that memory (e.g., if the default constructor didn’t allocate
a large enough pool of memory or if it opened the wrong file).
Conclusion: All other things being equal, your code will run faster if
you use initialization lists rather than assignment.
Regarding default initialization (such as Foo(): m_bar(5) { }) I would also go with the initializer list approach (or the C++11 member initialization approach) just for consistency with the above guideline.
This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 8 years ago.
I have been working in C++ for some time now, but I am unsure about the difference between the two options:
public : Thing(int _foo, int _bar): member1(_foo), member2(_bar){}
and
public : Thing(int _foo, int _bar){
member1 = _foo;
member2 = _bar;
}
I have a feeling that they do the same thing, but is there a practical difference between these 2 syntaxes. Is one of these safer then the other, and do they handle default parameters differently.
Not totally accustomed to the first example so if I made a mistake on it I apologize.
They are not the same if member1 and member2 are non-POD (i.e. non-Plain Old Data) types:
public : Thing(int _foo, int _bar){
member1 = _foo;
member2 = _bar;
}
is equivalent to
public : Thing(int _foo, int _bar) : member1(), member2(){
member1 = _foo;
member2 = _bar;
}
because they will be initialized before the constructor body starts executing, so basically twice the work is done. That also means, if the type of these members don't have default constructor, then your code will not compile.
The first one is the recommended best practice, as it is more idiomatic and avoids re-initializing fields for types which do have a default constructor (i.e. non-primitive types).
When you only initialize a member inside the constructor body, the compiler generates a default member initialization statement for you if it can, so you end up doubly initializing it. This may not be a big deal in some cases, but may be serious performance overhead if constructing the object is expensive.
Update
However, user defined types without a(n explicitly defined or generated) default constructor can't be initialized this way, so a compiler error is produced. The same is true for const and reference fields - these can only be initialized explicitly in the member initializer list.
The only thing to add to Péter Török answer is that the Initializer List is the only way to initialize const members of objects, i.e.:
class foo
{
public:
foo(int value)
: myConstValue(value)
{};
foo()
{
myConstValue = 0; // <=== Error! myConstValue is const (RValue), you can't assign!
};
private:
const int myConstValue;
}
In your example code, the first one in constructor initialization and second one is assignment inside constructor body.
Constructor initialization list is the best way to do all member initialization because it improves performance.
class A
{
string name;
public:
A(string myname):name(myname) {}
}
In above case compiler will not create a temporary object to do the initialization. However in the following case:
A::A()
{
name = myname;
}
A separate temporary object is created, and this temporary object is passed to string's assignment operator to assign to name. Then the temporary object is destroyed, which is not quite efficient.
Note: It is mandatory that a reference or a const member must be intialized in a constructor initialization list. They cannot be 'assigned' in the body of the constructor.
Apart from other answers, I would like mention that constructor initialization only for initialize
member variables.
class Demo
{
int a;
int b;
public:
Demo(int a,int b):a(a),b(b)
{
}
};
if we initialize a and b inside constructor it would be self assignment.
First is initialization, using an initializer list and the second is default construction and then assignment. The first one is at least as fast as the second one and is preferable to the second one.
With respect to the class definition for complex number, I saw two types of definitions:
Definition 1
class Complex
{
private:
double re;
double im;
public:
Complex(float r,float i) {re = r; im = i;}
~Complex() {};
};
Definition 2
class Complex
{
private:
double re;
double im;
public:
Complex(double r,double i): re(r), im(i) {}
~Complex() {};
};
The first definition looks OK to me but I do not quite understand the second definition, how does
Complex(double r,double i): re(r), im(i) {}
work? What does "re( )" mean?
It's called an initializer list. In a class's constructor, you can initialize member variables with this syntax. So in this, it's equivalent to putting the statements re = r; im = i; in the body of the constructor.
In the case of POD variables such as int, double, and pointer types, there is no difference between the initializer list syntax and regular assignments in the body. However, for const variables, references, and objects with non-trivial constructors, there is an important difference:
For const variables and reference variables, they must be initialized in the initializer list. They cannot be initialized by assigning to them in the body.
For objects with non-trivial constructors, how you initialize them in the initializer list corresponds to the constructor that gets called. If you omit the variable, then that object's default constructor gets called (if that object has no default constructor, then that is a compiler error).
Because of that, it's generally recommended that objects with constructors get initialized in the initializer list to avoid redundant work -- if you let its default constructor run by omitting it from the initializer list and then perform some sort of initialization in the constructor body, you're initializing it twice, which can be wasteful.
For example:
class Example
{
private:
std::string m_string;
public:
Example()
{
// m_string is first initialized by the std::string default constructor,
// then we assign to it with operator=(const char *).
// This is inefficient.
m_string = "test";
}
Example(int dummy)
: m_string("test")
{
// Better: we just call the std::string(const char*) constructor directly
}
};
The second form of the Complex constructor uses initialization lists, which are a different (and preferred way) of initialization class members.
The re(...) thing means that member field re should be constructed with whatever arguments it is passed.
As another example - you can create primitives like double and int like this:
double d(5.0d);
int i(5);
Which should explain how the parentheses work in the lists.
That's an initialization list. It sets the value of re to r and the value of im to i.
Generally you'll see a performance gain by using an initialization list, but it's also important to know when not to use it.
First of all, there should be a semicolon after the entire class definition in C++. Otherwise your code will fair to compile.
Anyway, the
Complex(double r,double i): re(r), im(i) {}
is a constructor for the Complex class that simply places the value of r into re and the value of i into im. That is another way of initializing a class's values.
Note that initialization lists like that can be very useful for initializing member classes within a class. Here's an example:
class MemberClass
{
private:
int mValue;
public:
MemberClass(int value): mValue(value) {}
};
class MemberHolder
{
private:
MemberClass mMember;
public:
MemberHolder(int value): mMember(value) {}
};
Initialization lists are important for using classes without a default constructor within other classes.
In C++ there is a distinction between assignment and initialization.
a = 5; // assignment
int b = 6; // initialization
int b(6); // also initialization
The first version of your class performs assignment inside the body of the constructor. This is more expensive, because the data members re and im are first default-constructed, and then are assigned their values.
In the second version constructor initialization list is used. Here the data members are initialized with the supplied values. This occurs in one step, while default constructor + assignment are two steps. This is more efficient.
Generally, you should prefer initializing your data members in the initialization list to assigning their values inside the body of the constructor. There is a caveat, though. The data member in the initialization list are initialized in the order in which they are declared in the class, not in the order in which they occur in the initialization list. Generally, you want the order of members in the list to match their order of declaration. Otherwise you may end up with very hard to find bugs if the initialization of one data member depends on the value of another.