433

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 data member, but I've never seen this syntax before. It looks like a function/constructor call but for an int. This makes no sense to me.

Are there any other esoteric language features like this, you'll never find in an ordinary C++ book?

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
nils
  • 4,649
  • 3
  • 19
  • 8
  • 114
    An "ordinary c++ book" that doesn't mention this is probably a c book where someone thought "++" would look cool on the cover ... – Rasmus Kaj Nov 10 '09 at 23:35
  • 100
    "you'll never find in a ordinary C++ book". Oh. Dear. Throw away your "ordinary C++ book" right now. Not out the window - somebody else might pick it up. Preferably shred it and put it for recycling. Done? Now consult http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list to get a new book. – Steve Jessop Nov 10 '09 at 23:36
  • 61
    This language feature is hardly esoteric. It's a fairly major feature of object construction. – Charles Salvia Nov 10 '09 at 23:36
  • 47
    In fact, far from esoteric, you often have no choice but to use initializer lists. For example, if your class contains a `const` member variable, or a reference, you have to use an initalizer list. – Charles Salvia Nov 10 '09 at 23:42

14 Answers14

404
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.

orestisf
  • 1,396
  • 1
  • 15
  • 30
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 13
    @nils This is the best answer so far. The order of the initialization Als pointed out is also extremely important, while the Visual Studio compiler won't say anything about that, other compiler like gcc will fail. It is also important to note depending your compiler and the situation it is **not Always true** that this will improve performance or be more efficient. – ForceMagic Mar 14 '12 at 07:21
  • If I have a constructor with a lot of variables says more than 10, will that be inconvenient to list all of them in that list order? – ryf9059 Jan 07 '13 at 12:06
  • 1
    @ryf9059: Why do you think it will be inconvenient? You have to list them anyways, so why not in order same as that of declaration. – Alok Save Jan 07 '13 at 12:10
  • You may want to clarify some of your terminology, [this question](http://stackoverflow.com/q/31275680/1708801) was asked in order to clarify some of the details in your answer. The details are somewhat orthogonal to the original question asked here. – Shafik Yaghmour Jul 07 '15 at 18:44
  • 2
    this should have been the answer. thank god i scrolled down else i would have missed it. – Coffee_lover Aug 13 '15 at 11:41
  • @ryf9059 Your point makes sense to me considering length of one line. I guess the initialization can be broken into multiple lines in case the list is lengthly so that inconvenience you are probably mentioning can be avoided. – Rajesh Mar 15 '18 at 00:33
  • @AlokSave You said MyClass2 and MyClass doesn't have a default constructor. Default constructor you mean user defined no argument constructor? – Rajesh Mar 15 '18 at 00:56
  • 1
    @AlokSave __MyClass(int a, int b, int c) : i(a), b(b), k(c) {__ // Without Member Initializer // this->b = b; __}__ it should be like this __MyClass(int `&a` , int b, int c) : i(a), b(b), k(c) {__ // Without Member Initializer // this->b = b; __}__ and so respective changes in declaration and call. without this change `i` will refer to `a` but `a` can't refer to `x` as it contain just value of `x` so indirectly `i` can't refer to `x`. so if we modify value of `i` then it just modify `a` but not `x` – Abhishek Mane May 15 '21 at 11:42
  • 1
    @AbhishekMane You are right, and here is a link to the related question showing it: https://stackoverflow.com/q/67619383/3150802 – Peter - Reinstate Monica May 20 '21 at 12:08
247

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.

TallChuck
  • 1,725
  • 11
  • 28
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Knowing the terminology is critical - I'm jealous that I didn't think of it. – Mark Ransom Nov 10 '09 at 23:36
  • 1
    There are also a bunch of other reasons for using init lists. especially when the order of initialisation matters. It's a shame it has such a stupid fake function call syntax. – Martin Beckett Nov 10 '09 at 23:50
  • You should *always* initialize member variables, unless it's *really* okay for them to contain garbage after their parent object is instantiated (e.g., I/O buffers). Because you're guaranteed to get garbage in them if you don't. – David R Tribble Nov 11 '09 at 00:15
  • 21
    @mgb, the initialization list does not determine the order of initialization. Member variables are initialized in the order they are declared in the class, even if that differs from the order of the initializations on the constructor. – ScottJ Nov 11 '09 at 00:26
  • 14
    @mgb: I don't think it's intended as a fake function call syntax. It's initialization syntax, like `int i(23);`, `std::vector emptyVec(0);`, `std::vector fullVec(10,23.);`, etc. Only with the type removed, of course, because the type is in the member declaration. – Steve Jessop Nov 11 '09 at 01:03
  • 1
    @Martin : It doesn't have a function call syntax, it has a construction syntax (ala: new String("Name")). It fits in with the constructor better than Foo(int num) : m_Count = 5. Not to mention that classes must be constructed at this point anyway, since it's initialized here. Foo(int num) : Bar = num, wouldn't compile right. It just seems weird seeing Foo(int num) : m_Count(num), since primitive types aren't constructed. – Lee Louviere Apr 22 '11 at 20:33
  • @Steve, in the init list it almost makes sense - but I don't like the fashion for using it in code. `int name(10)` isn't as clear as `int name=10;` as far as i'm concerned – Martin Beckett Apr 22 '11 at 23:17
  • Prefer "member initialisation list" or `ctor-initializer`, please. "Initialization list" is ambiguous and confusing when you consider `{1,2,3,4,5}`. – Lightness Races in Orbit Jul 17 '11 at 15:56
18

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Josh
  • 3,540
  • 1
  • 21
  • 12
  • 7
    I wouldn't say that this is about efficiency. It is about providing a way to initialize something that requires initialization, but cannot be default-initualized. For some reason people mention constants and references as the examples, while the most obvious example would be classes without default constructors. – AnT stands with Russia Nov 11 '09 at 00:26
  • 1
    We're both right; in his example you could argue for efficiency; for the const/reference/no default constructor issue it's both efficiency and necessity. I upvoted an answer below because of it :) [Farnsworth voice] It can do other things. Why shouldn't it? – Josh Nov 11 '09 at 04:19
  • 1
    `Bar bar(); // default constructor` Are you sure? – Lightness Races in Orbit Dec 27 '15 at 14:55
  • @LightnessRacesinOrbit Just curious to know : What should that be according to you? – asn Aug 02 '19 at 11:01
16

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.

LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
9

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.

wkl
  • 77,184
  • 16
  • 165
  • 176
8

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.

Destructor
  • 14,123
  • 11
  • 61
  • 126
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • But bjarne stroustrup in his book TC++PL & The C++ Programming language says that _"Built-in types also have default constructors"_ . http://www.geeksforgeeks.org/c-default-constructor-built-in-types/ & http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15 also says _built in types have constructors_. I personally asked this question to bjarne via mail & he says me that yes, built in types also have constructors. so your answer is wrong !!! – Destructor May 11 '16 at 04:54
  • @Destructor: My answer is absolutely correct. Bjarne Stroustrup deliberately and explicitly **lied** in his book for the sake of simplifying it. Compare the size of the TC++PL book and the size of C++ language standard. See the difference? The price to pay for relative compactness of TC++PL is such obvious (and well-known to everyone) errors and omissions as the one you mentioned (there are quite a few others as well). So, to put it more succinctly: my answer is right, TC++PL is wrong. But for a person like you, who is just beginning to learn, TC++PL is good enough. – AnT stands with Russia May 11 '16 at 14:43
  • And please don't tell us trollish fairy tales about "asked this question to bjarne via mail". This issue is, again, well-known and has been exhaustively discussed and closed a long time ago. (But even if he said something like that to you, it wouldn't matter anyway.) In the end, the only thing that matters is what the language specification says. What Bjarne Stroustrup says is irrelevant. The post on geeksforgeeks you linked is completely bogus. – AnT stands with Russia May 11 '16 at 14:52
  • So, you think that standard is bug free? C++ standard has also bugs. – Destructor May 11 '16 at 14:54
  • @Destructor: Of course, it does. However, the standard *defines* the language. Everything the standard says is The Absolute Truth by definition. The only "bugs" it can possibly have are mostly wording-related things like self-contradictory wording, ambiguous wording, under-specified and so forth. Such bugs are vehemently sought out, reported, documented, discussed and resolved. "Bugs" in the intent can also exist, but they are a matter of debate. – AnT stands with Russia May 11 '16 at 15:26
  • The concept of "initialization" and the role of "constructors" has been around for a long time. The intent is clear and the corresponding wording is clear as well. There are no bugs there. And even if there are bugs in the wording, the intent is, again, perfectly clear. The intent is not being debated. You want to challenge this intent - you can submit a proposal to the committee. But at this moment in C++ only class types can have constructors. – AnT stands with Russia May 11 '16 at 15:27
  • Ok, thank you sir for taking your time for this wonderful in depth explanation !!! – Destructor May 11 '16 at 16:24
6

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
6

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.

nos
  • 223,662
  • 58
  • 417
  • 506
5

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.

Aric TenEyck
  • 8,002
  • 1
  • 34
  • 48
  • 8
    The benefit is that it's usually more efficient. And, in some cases, such as when you have `const` member variables, or member variables that are references, you *have* to use an initializer list. – Charles Salvia Nov 10 '09 at 23:35
5

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

pm100
  • 48,078
  • 23
  • 82
  • 145
5

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.

suszterpatt
  • 8,187
  • 39
  • 60
3

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
3

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.

Karen Baghdasaryan
  • 2,407
  • 6
  • 24
1

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) {.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265