ctor-initializer

Member initialization while using delegated constructor

大憨熊 提交于 2019-11-29 21:59:39
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 this: hpp: class Tokenizer { public: Tokenizer(); Tokenizer(std::stringstream *lines); virtual ~Tokenizer() {}; private: std::stringstream *lines; }; cpp: Tokenizer::Tokenizer() : expected('=') { } Tokenizer::Tokenizer(std::stringstream *lines) : Tokenizer(), lines(lines) { } But this is giving me the error: In constructor ‘config::Tokenizer::Tokenizer(std

Variables after the colon in a constructor [duplicate]

一曲冷凌霜 提交于 2019-11-28 10:45:08
This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers 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 constructor's parameters? It's a way of invoking the constructors of members of the point3 class. if x,y, and z are floats, then this is just a more efficient way of writing this point3( float X, float Y, float Z): { x = X; y = Y; z

Initializer list *argument* evaluation order

倖福魔咒の 提交于 2019-11-28 09:12:21
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 order in which the arguments to those initializations are evaluated. I'm working with a system that frequently passes references to serialization objects around, and wondering if I can ensure that bits are read from it in the right order, independent of the order in which those bits get written into the object's fields. struct Foo { int a; double b;

Variables after the colon in a constructor [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 03:46:52
问题 This question already has answers here : What is this weird colon-member (“ : ”) syntax in the constructor? (12 answers) Closed 5 years ago . 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 constructor's parameters? 回答1: It's a way of invoking the constructors of members of the point3 class. if x

How to initialize a const field in constructor?

泄露秘密 提交于 2019-11-26 16:31:47
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, I thought I could write like the code below but it does not compile.. class Foo; class Bar { public: Foo * const foo; Bar(Foo* foo) { this->foo = foo; } }; class Foo { public: int a; }; Any suggestion is welcome. You need to do it in an initializer list: Bar(Foo* _foo) : foo(_foo) { } (Note that I renamed the incoming variable to avoid confusion.) I

Initializing a member array in constructor initializer

廉价感情. 提交于 2019-11-26 14:31:01
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] = {1,3,4}; Questions How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor initializers? Or the invalidness of the above code is a corollary of some other rules? Do C++0x initializer

How to initialize a const field in constructor?

天涯浪子 提交于 2019-11-26 12:15:31
问题 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, I thought I could write like the code below but it does not compile.. class Foo; class Bar { public: Foo * const foo; Bar(Foo* foo) { this->foo = foo; } }; class Foo { public: int a; }; Any suggestion is welcome. 回答1: You need to do it in an

What does a colon following a C++ constructor name do? [duplicate]

旧城冷巷雨未停 提交于 2019-11-26 11:34:40
This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers 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; }; This is an initialization list , and is part of the constructor's implementation. The constructor's signature is: MyClass(); This means that the constructor can be called with no parameters. This makes it a default constructor , i.e., one which will be

Initializing a member array in constructor initializer

六月ゝ 毕业季﹏ 提交于 2019-11-26 03:56:32
问题 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] = {1,3,4}; Questions How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor

What does a colon following a C++ constructor name do? [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 02:28:01
问题 This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers 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; }; 回答1: This is an initialization list , and is part of the constructor's implementation. The constructor's signature is: MyClass(); This means that