Questions tagged [member-initialization]

121 questions
5
votes
3 answers

Initializer list syntax in member initializer list using C++11

I've been going through 'A Tour of C++' and Bjarne uses the the c++11 initializer list feature in member initialization in a constructor, like so (using curly brackets): A a; B b; Foo(Bar bar): a{bar.a}, b{bar.b} {} This, however doesn't compile…
G.Rassovsky
  • 774
  • 1
  • 10
  • 23
5
votes
4 answers

Is there an Objective-C/C++ equivalent to C#'s member brace-initialization capability?

In addition to 'var' (see my other post here), one of the things I really like about C# is that I can both declare, then initialize members of a class using braces, like this... var reallyLongFooVarName = new ReallyLongFooClassName(){ Name =…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
5
votes
5 answers

Illegal Member initialization

I am using this pretty simple class without using any inheritance. class A { int a; int b; public: A(int x, int y) { a = x; b = y;} A() :A(0,0){}; ~A(){}; } ; int main () { A a1, a2(5, 7) ; } I get this error. error C2614: 'A' : illegal…
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
5
votes
1 answer

Clang++ Xcode 4.4 Non Static Member Initialization and Move constructor

I am using Xcode 4.4 with mountain lion. I can't seems to understand why non-static member initalization in templates invokes a move constructor for the variable. Is there anyway to overcome this error? Example Code: #include #include…
4
votes
1 answer

C++0x Member initialization without a constructor

In N3257 I found an example using initializing members without a constructor, which is fine. I guess that is possible, because it is a POD. template struct adaptor { NonStdContainer* ptr; // <- data member T*…
towi
  • 21,587
  • 28
  • 106
  • 187
4
votes
2 answers

Initializing from a pair in constructur intializer list

I have a function f returning a pair of objects of classes A and B: auto f() -> std::pair; Furthermore, I have a class C that has A and B as members: class C { public: C(); private: A a; B b; }; In the constructor of C, I want…
Henk
  • 826
  • 3
  • 14
4
votes
2 answers

Memberwise Initialization

Possible Duplicate: C++ initialization lists What is the difference between member-wise initialization and direct initialization in a class? What is the difference between the two constructors defined in the class? class A { public: int…
cppcoder
  • 22,227
  • 6
  • 56
  • 81
4
votes
2 answers

How do I forward the values of tuple to a member initializer?

I need to forward the values of a tuple to a member initializer: struct Struct { Member1 member1; Member2 member2; template Struct( Tuple1&& tuple1, Tuple2&& tuple2 ) : member1(tuple1...),…
Helloer
  • 417
  • 3
  • 13
4
votes
2 answers

Is it allowed to call a non-static member function in a default member initializer?

Consider this class: #include struct foo { int a = 42; int b = bar(); int bar() { return a; } }; int main(){ foo f; std::cout << f.a << " " << f.b; } It prints the expected 42 42. Is it allowed by the standard to…
4
votes
3 answers

Why use member init lists if there is default member initialization

In C++11, default member initialization was introduced. So, I just wanted to ask that why member initializer lists must still be employed in favour of these? E.g. If this is allowed class apple { int i = 10; }; Why should this be used class…
Gaurav Pant
  • 962
  • 10
  • 30
4
votes
3 answers

Order of member initializers

Following code gives correct output, If I declare variables i and j, Like int i, j; class A { int i, j; public: A(int val) : i(val), j(i + 1) { cout<
msc
  • 33,420
  • 29
  • 119
  • 214
4
votes
1 answer

Initialization of member: bug in GCC or my thinking?

I've got an enum type defined in the private section of my class. I have a member of this type defined as well. When I try to initialize this member in the constructor body, I get memory corruption problems at run-time. When I initialize it through…
San Jacinto
  • 8,774
  • 5
  • 43
  • 58
4
votes
2 answers

Using std::function in member initialization list

I have a typedef: typedef S32(iMyDataClass1::*getDataFunction_t)(void); and a type: struct functionMap_t { std::vector pDataFunctionTable; struct dataRequestor_t dataRequestor; }; I then have a member…
Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54
4
votes
2 answers

Member initialization lists with default arguments

Is it possible use default arguments with member initialization lists? Vector3::Vector3(double xI, double yI, double zI) : x(xI=0), y(yI=0), z(zI=0) { } The constructor always sets x, y, and z to 0 even if you call it with setting the arguments.
Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57
3
votes
2 answers

How do I set a default value for a class member with something other than its initializer list constructor

Let's say I have something like this: class A { public: A(int x, int y) { std::cout << "Constructed from parameters" << std::endl; } // Non-copyable, non-movable A(A const&) = delete; A(A&&) = delete; A&…
Eternal
  • 2,648
  • 2
  • 15
  • 21
1
2
3
8 9