virtual-inheritance

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

“Middle classes” in diamond inheritance graph using non-default virtual base constructor: why is it not a compile error?

偶尔善良 提交于 2021-02-08 09:58:08
问题 Consider a diamond inheritance graph (i.e., virtual base class). We know from previous questions that on construction the most derived class directly calls the default (0-arg) constructor of the (virtual) base. But we also know from answers to the previous question (e.g., here that if the "middle" classes in the diamond have constructors that are used by the most-derived class and those constructors "call" non-default constructors of their (virtual) base class (via the initialization list)

“Middle classes” in diamond inheritance graph using non-default virtual base constructor: why is it not a compile error?

南笙酒味 提交于 2021-02-08 09:56:53
问题 Consider a diamond inheritance graph (i.e., virtual base class). We know from previous questions that on construction the most derived class directly calls the default (0-arg) constructor of the (virtual) base. But we also know from answers to the previous question (e.g., here that if the "middle" classes in the diamond have constructors that are used by the most-derived class and those constructors "call" non-default constructors of their (virtual) base class (via the initialization list)

Does virtual inheritance force a base class to be default constructible?

邮差的信 提交于 2021-02-07 13:17:11
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

Does virtual inheritance force a base class to be default constructible?

让人想犯罪 __ 提交于 2021-02-07 13:16:33
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

C++ code segfaults when compiled -O with Apple's LLVM compiler, but not with g++ -7.2.0

我的未来我决定 提交于 2021-02-06 10:20:10
问题 Update: I've created an even more M, but still CVE that reproduces the crash. Summary: removed all use of the Bool* bools_ field in Base class (but it still must be defined or the crash does not happen). Also removed Base::Initialize() and the virtual method Rule from Base and its descendants. New MCVE is attached. I've managed to create an MCVE for this code and posted it below. Some descriptive details: the code uses virtual base and derived classes, and certain derived classes that are

How does the virtual inheritance table work in g++?

北战南征 提交于 2021-01-28 08:25:38
问题 I'm trying to get a better understanding how virtual inheritance works in practice (that is, not according to the standard, but in an actual implementation like g++ ). The actual question is at the bottom, in bold face. So, I built myself a inheritance graph, which has among other things, these simple types: struct A { unsigned a; unsigned long long u; A() : a(0xAAAAAAAA), u(0x1111111111111111ull) {} virtual ~A() {} }; struct B : virtual A { unsigned b; B() : b(0xBBBBBBBB) { a = 0xABABABAB; }

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

How can I check the types of derived classes in C++?

≯℡__Kan透↙ 提交于 2020-03-03 08:25:51
问题 Let's say I have some base abstract class and three different classes that derive and implement its methods. Is there is a 'Type' object like as in C#? Or in other words, how do I get instances of all these classes? #ModuleBase.cpp class ModuleBase { }; #Module1.cpp class Module1 : public virtual ModuleBase { }; #Module2.cpp class Module2 : public virtual ModuleBase { }; #Module3.cpp class Module3 : public virtual ModuleBase { }; 回答1: You can create instanceof like methods that can detect the