Questions tagged [ctor-initializer]

Constructor initializer list. In C++, the initializer list is the place where initialization of the object should occur. Is where the constructors for base classes and members are called. Members are initialized in the same order as they are declared, not as they appear in the initializer list.

Constructor initializer list. The initializer list is the place where initialization of the object should occur. Is where the constructors for base classes and members are called. Members are initialized in the same order as they are declared, not as they appear in the initializer list.

If a parameter in the constructor has the same name as one of the members, the ambiguity of that identifier being passed in a constructor call inside the initializer list is resolved choosing the parameter (and not the member).

Members or base classes not present in the list will be default constructed.

Source:http://en.cppreference.com/w/cpp/language/initializer_list

74 questions
433
votes
14 answers

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

Recently I've seen an example like the following: #include 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?…
nils
  • 4,649
  • 3
  • 19
  • 8
198
votes
6 answers

Does a const reference class member prolong the life of a temporary?

Why does this: #include #include using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout << "The answer…
Kyle
  • 4,487
  • 3
  • 29
  • 45
123
votes
8 answers

Initializing a member array in constructor initializer

class C { public: C() : arr({1,2,3}) //doesn't compile {} /* C() : arr{1,2,3} //doesn't compile either {} */ private: int arr[3]; }; I believe the reason is that arrays can be initialized only with = syntax, that is: int arr[3] =…
121
votes
1 answer

Member initialization while using delegated constructor

I've started trying out the C++11 standard and i found this question which describes how to call your ctor from another ctor in the same class to avoid having a init method or the like. Now i'm trying the same thing with code that looks like…
lfxgroove
  • 3,778
  • 2
  • 23
  • 33
106
votes
9 answers

What does a colon following a C++ constructor name do?

What does the colon operator (":") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0);? class MyClass { public: MyClass() : m_classID(-1), m_userdata(0) { } int m_classID; void *m_userdata; };
spencewah
  • 2,196
  • 4
  • 20
  • 31
82
votes
6 answers

How to initialize a const field in constructor?

Imagine I have a C++ class Foo and a class Bar which has to be created with a constructor in which a Foo pointer is passed, and this pointer is meant to remain immutable in the Bar instance lifecycle. What is the correct way of doing it? In fact,…
puccio
  • 3,054
  • 8
  • 29
  • 20
31
votes
3 answers

Variables after the colon in a constructor

I am still learning C++ and trying to understand it. I was looking through some code and saw: point3(float X, float Y, float Z) : x(X), y(Y), z(Z) // <----- what is this used for { } What is the meaning of the "x(X), y(Y), z(Z)" sitting beside the…
numerical25
  • 10,524
  • 36
  • 130
  • 209
28
votes
5 answers

What's the differences between member initializer list and default member initializer on non-static data member?

I'd like to understand what's the differences of using one form rather than the other (if any). Code 1 (init directly on variables): #include using namespace std; class Test { public: Test() { cout<< count; } …
markzzz
  • 47,390
  • 120
  • 299
  • 507
27
votes
2 answers

The 'this' pointer in the initialization list of the constructor

I guess I am unable to understand why this is not working. I always thought that I can use 'this' pointer inside the constructor, but I never knew that I cannot use 'this' in the initialization list. #include class A { public: …
Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45
25
votes
5 answers

Initializing a reference to member to NULL in C++

Is it possible to initialize a reference member to NULL in c++? I'm trying to something like this: class BigClass { private: Object m_inner; public: const Object& ReadOnly; BigClass() : ReadOnly(NULL) { Do stuff. } }; I…
Idov
  • 5,006
  • 17
  • 69
  • 106
24
votes
2 answers

Does incrementing in a member initializer list generate undefined behavior?

Is this causing undefined behaviour? Specifically, the incrementing in the initializer list and how will that be evaluated. class Wrinkle { public: Wrinkle(int i) : a(++i), b(++i), x(++i) {} private: int a; int x; int b; }; The…
20
votes
3 answers

Dependencies in Initialization Lists

Is this behavior well-defined? class Foo { int A, B; public: Foo(int Bar): B(Bar), A(B + 123) { } }; int main() { Foo MyFoo(0); return 0; }
15
votes
1 answer

Initializer list *argument* evaluation order

So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the…
12
votes
3 answers

C++: Should I initialize pointer members that are assigned to in the constructor body to NULL?

Suppose I have: // MyClass.h class MyClass { public: MyClass(); private: Something *something_; } // MyClass.cpp MyClass::MyClass() { something_ = new Something(); } Should I initialize something_ to NULL (or 0) in the constructor…
User
  • 62,498
  • 72
  • 186
  • 247
12
votes
8 answers

Complex initialization of const fields

Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1; const double ImportantConstant2; const double ImportantConstant3; private: void…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
1
2 3 4 5