ctor-initializer

Initializer list *argument* evaluation order

不羁的心 提交于 2019-12-17 18:55:51
问题 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,

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

断了今生、忘了曾经 提交于 2019-12-12 01:57:06
问题 Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout << "The answer is: " << sandbox.member << endl; return 0; } Give output of: The answer is: Instead of: The answer is: four 回答1: Only local const references prolong the lifespan. The standard specifies such behavior in §8.5.3/5, [dcl.init.ref], the section on initializers of

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

狂风中的少年 提交于 2019-12-07 03:54:08
问题 Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C: virtual A { public: C() {cout << "5";} C(const C & obj) {cout << "6";} }; class D:B,C { public: D() {cout << "7";} D(const D & obj) {cout << "8";} }; int main() { D d1; cout << "\n"; D d(d1); } The output of the program is below: 1357 1358 So, for line D d(d1) the copy

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

我的梦境 提交于 2019-12-05 07:49:00
Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C: virtual A { public: C() {cout << "5";} C(const C & obj) {cout << "6";} }; class D:B,C { public: D() {cout << "7";} D(const D & obj) {cout << "8";} }; int main() { D d1; cout << "\n"; D d(d1); } The output of the program is below: 1357 1358 So, for line D d(d1) the copy constructor of D class is bein called. During inheritance we need to explicitly call copy constructor

Dependencies in Initialization Lists

独自空忆成欢 提交于 2019-12-04 17:01:20
问题 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; } 回答1: No, it's undefined. A will be initialized first (it's first in the class definition), and it uses uninitialized B . Class members are initialized in the order they appear in the class definition, irrespective of their order in the initialization list. Indeed, it is bad practice to mismatch the member definition order with the initialization list

why are virtual base non-default constructors not called unless most-derived base explicitly invokes them?

倖福魔咒の 提交于 2019-12-04 03:33:09
问题 I would like to understand WHY C++ standard mandates that virtual base non-default constructors cannot be invoked by an intermediate NOT most-derived class, as in this code, when compiled with '-D_WITH_BUG_' : /* A virtual base's non-default constructor is NOT called UNLESS * the MOST DERIVED class explicitly invokes it */ #include <type_traits> #include <string> #include <iostream> class A { public: int _a; A(): _a(1) { std::cerr << "A() - me: " << ((void*)this) << std::endl; } A(int a): _a

Dependencies in Initialization Lists

旧街凉风 提交于 2019-12-03 11:00:51
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; } No, it's undefined. A will be initialized first (it's first in the class definition), and it uses uninitialized B . Class members are initialized in the order they appear in the class definition, irrespective of their order in the initialization list. Indeed, it is bad practice to mismatch the member definition order with the initialization list order. If your instance of Foo happened to have static duration, like in Foo f(0); int main(){} , the behavior

Ctor Initializer: self initialization causes crash?

戏子无情 提交于 2019-12-02 03:51:16
问题 I had a hard time debugging a crash on production. Just wanted to confirm with folks here about the semantics. We have a class like ... class Test { public: Test() { // members initialized ... m_str = m_str; } ~Test() {} private: // other members ... std::string m_str; }; Someone changed the initialization to use ctor initialization-lists which is reasonably correct within our code semantics. The order of initialization and their initial value is correct among other things. So the class looks

why are virtual base non-default constructors not called unless most-derived base explicitly invokes them?

邮差的信 提交于 2019-12-01 18:45:54
I would like to understand WHY C++ standard mandates that virtual base non-default constructors cannot be invoked by an intermediate NOT most-derived class, as in this code, when compiled with '-D_WITH_BUG_' : /* A virtual base's non-default constructor is NOT called UNLESS * the MOST DERIVED class explicitly invokes it */ #include <type_traits> #include <string> #include <iostream> class A { public: int _a; A(): _a(1) { std::cerr << "A() - me: " << ((void*)this) << std::endl; } A(int a): _a(a) { std::cerr << "A(a) - me:" << ((void*)this) << std::endl; } virtual ~A() { std::cerr << "~A" << (

Member initialization while using delegated constructor

蓝咒 提交于 2019-11-30 10:44:04
问题 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) :