Questions tagged [member-initialization]

121 questions
201
votes
8 answers

How do C++ class members get initialized if I don't do it explicitly?

Suppose I have a class with private memebers ptr, name, pname, rname, crname and age. What happens if I don't initialize them myself? Here is an example: class Example { private: int *ptr; string name; string *pname; …
bodacydo
  • 75,521
  • 93
  • 229
  • 319
41
votes
2 answers

Why can't member initializers use parentheses?

For example, I cannot write this: class A { vector v(12, 1); }; I can only write this: class A { vector v1{ 12, 1 }; vector v2 = vector(12, 1); }; Why is there a difference between these two declaration syntaxes?
delphifirst
  • 1,781
  • 1
  • 14
  • 23
31
votes
6 answers

C++ Member Initializer List

Please explain how to use member initializer lists. I have a class declared in a .h file and a .cpp file like this: class Example { private: int m_top; const int m_size; /* ... */ public: Example(int size, int grow_by = 1) :…
Yesman
  • 437
  • 1
  • 4
  • 5
22
votes
4 answers

Can array members be initialized self-referentially?

Consider the following code in which we initialize part of D based on another part of D: struct c { c() : D{rand(), D[0]} {} int D[2]; }; int main() { c C; assert(C.D[0] == C.D[1]); } Is the above program well-defined? Can we…
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
19
votes
1 answer

"Invalid use of non-static data member" when initializing static member from global variable

class A { int x; static int i; }; int x = 10; int A::i = x; When I compile the code above, it get the error :8:12: error: invalid use of non-static data member 'A::x' 8 | int A::i = x; | ^ :2:9: note:…
YuCL Lan
  • 193
  • 1
  • 5
18
votes
1 answer

Using lambda in default initializer gcc vs clang

#include #include int main() { struct point_of_cone { double x, y; double z = [&] { using std::sqrt; return sqrt(x * x + y * y); }(); }; point_of_cone p = {3.0, 4.0}; assert(p.z ==…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
15
votes
5 answers

What is "member initializer" in C++11?

I run across a weird concept named "member initializer". Here says: C++11 added member initializers, expressions to be applied to members at class scope if a constructor did not initialize the member itself. What is its definition? Are there…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
13
votes
6 answers

Right way to conditionally initialize a C++ member variable?

I'm sure this is a really simple question. The following code shows what I'm trying to do: class MemberClass { public: MemberClass(int abc){ } }; class MyClass { public: MemberClass m_class; MyClass(int xyz) { if(xyz == 42) …
Pedro d'Aquino
  • 5,130
  • 6
  • 36
  • 46
9
votes
2 answers

Brace-or-equal-initializers in anonymous struct does not work on VS2013

Brace-or-equal-initializers in an anonymous struct within a struct doesn't do their work on output produced by VS2013. There's the code: #include #include struct S { struct { uint64_t val = 0; …
user2694310
9
votes
3 answers

Are member-initialization lists really more efficient?

I agree with the consensus that it's generally best to initialise C++ data members in a member initialization list rather than the body of a constructor, but I am sceptical of this explanation The other (inefficient) way to build constructors is…
spraff
  • 32,570
  • 22
  • 121
  • 229
7
votes
2 answers

Execute checks before initialization list

I have a member of class A in my own class which constructor takes multiple parameters. Im forwarding parameters of my own class to the constructor of class A. But its important that these parameters are correct, so i need to check them before…
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
7
votes
2 answers

Is gcc wrong to allow the initialization of a const array member with another array reference?

While (re)implementing a simple constexpr map, I wrote this (godbolt): template class flat_map { private: struct pair { key_type key; value_type value; }; const pair…
MatG
  • 574
  • 2
  • 7
  • 19
6
votes
2 answers

Is it possible to have a function-try-block per member initialiser?

During the member initialisation of a class with multiple members, it seems desirable to be able to catch an exception generated by any specific member initialiser, to wrap in additional context for rethrowing, but the syntax of a function-try-block…
Adam Barnes
  • 2,922
  • 21
  • 27
6
votes
2 answers

Should trivial default constructor respect default member initializer here?

Consider the code: #include #include struct stru { int a{}; int b{}; }; int main() { std::atomic as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
6
votes
2 answers

How to initialize const circular reference members

For example, I have two classes class Foo; class Bar; class Foo { const Bar &m_bar; ... }; class Bar { const Foo &m_foo; ... }; Let foo is object of Foo and bar is object of Bar. Is there any way (normal or "hacking") to create/initialize…
1
2 3
8 9