ctor-initializer

Deriving class from virtual base with no default constructor

血红的双手。 提交于 2021-02-11 07:24:13
问题 I'm writing a small hierarchy of exception classes for a C++ application I'm developing, and I'm having trouble deriving indirectly from std::runtime_error . Here is code analogous to what I've written so far: class RuntimeException : public virtual boost::exception, public virtual std::runtime_error { public: virtual ~RuntimeException() {} RuntimeException() : runtime_error("A RuntimeException occurred.") {} RuntimeException(const std::string& what) : runtime_error(what) {} }; class

Why do the constructor of the derived classes want to initialize the virtual base class in C++?

泪湿孤枕 提交于 2020-12-08 06:16:26
问题 My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor. Here is a simple example I made: class A { protected: A(int foo) {} }; class B: public virtual A { protected: B() {} }; class C: public virtual A { protected: C() {} }; class D: public B, public C { public: D(int foo, int bar) :A(foo) {} }; int main() { return 0; } For some reason, the constructors B::B() and C::C() are trying to initialize A (which, again

Why do the constructor of the derived classes want to initialize the virtual base class in C++?

坚强是说给别人听的谎言 提交于 2020-12-08 06:15:43
问题 My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor. Here is a simple example I made: class A { protected: A(int foo) {} }; class B: public virtual A { protected: B() {} }; class C: public virtual A { protected: C() {} }; class D: public B, public C { public: D(int foo, int bar) :A(foo) {} }; int main() { return 0; } For some reason, the constructors B::B() and C::C() are trying to initialize A (which, again

Throw exception from constructor initializer

耗尽温柔 提交于 2020-02-01 04:55:27
问题 What is the best way to throw exception from the constructor initializer? For example: class C { T0 t0; // can be either valid or invalid, but does not throw directly T1 t1; // heavy object, do not construct if t0 is invalid, by throwing before C(int n) : t0(n), // throw exception if t0(n) is not valid t1() {} }; I thought maybe making wrapper, e.g. t0(throw_if_invalid(n)) . What is the practice to handle such cases? 回答1: There are multiple ways of going about this, I think. From what I

What does the colon mean in a constructor? [duplicate]

萝らか妹 提交于 2020-01-03 09:08:13
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: C++ weird constructor syntax Variables After the Colon in a Constructor What does a colon ( : ) following a C++ constructor name do? For the C++ function below: cross(vector<int> &L_, vector<bool> &backref_, vector< vector<int> > &res_) : L(L_), c(L.size(), 0), res(res_), backref(backref_) { run(0); } What does the colon (":") tell the relationships between its left and right part? And possibly, what can be

Is taking the address of a member of an uninitialized object well defined?

心已入冬 提交于 2019-12-19 08:14:07
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

Is taking the address of a member of an uninitialized object well defined?

南笙酒味 提交于 2019-12-19 08:13:04
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

Initializing members with members

做~自己de王妃 提交于 2019-12-18 06:49:16
问题 This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C( ) : B( m_ObjectA ) { } Since the order of initialization is not defined, m_ObjectA might be uninitialized when the constructor of m_ObjectB is called, resulting in undefined behavior.

Initializing members with members

旧巷老猫 提交于 2019-12-18 06:49:11
问题 This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C( ) : B( m_ObjectA ) { } Since the order of initialization is not defined, m_ObjectA might be uninitialized when the constructor of m_ObjectB is called, resulting in undefined behavior.

May I call a virtual function to initialize a base-class sub-object?

血红的双手。 提交于 2019-12-17 21:09:14
问题 I know that virtual functions should not be called either directly or indirectly in a constructor, but this code runs fine. Is what I have here safe? #include <iostream> #include <string> struct A { A (const std::string& name) {std::cout << name << std::endl;} virtual std::string tag() const = 0; }; struct B: A { B() : A (tag()) {} virtual std::string tag() const override {return "B";} }; int main() { B b; // Output gives "B\n" } If not, would the following (based on a comment) be a correct