virtual-inheritance

Placement new crashing when used with virtual inheritance hierarchy in Visual C++

跟風遠走 提交于 2019-12-24 07:04:07
问题 I am using virtual inheritance with a selection of classes in c++. It is currently crashing on destruction. It seems to compile fine in the online compilers, however when I run in Visual Studio, it crashes. I have a pure virtual base class, which is being inherited virtually by its implementation. I then have a third class that is inheriting from the implementation regularly. I am using an internal system for creating and releasing memory. Under the hood it is using a placement new with a

Virtual Inheritance Confusion

安稳与你 提交于 2019-12-23 09:59:42
问题 I'm reading about inheritance and I have a major issue that I haven't been able to solve for hours: Given a class Bar is a class with virtual functions, class Bar { virtual void Cook(); }; What is the different between: class Foo : public Bar { virtual void Cook(); }; and class Foo : public virtual Bar { virtual void Cook(); }; ? Hours of Googling and reading came up with lots of information about its uses, but none actually tell me what the difference between the two are, and just confuse me

Inheriting constructors and virtual base classes

旧城冷巷雨未停 提交于 2019-12-23 09:12:19
问题 I'm about to create an exception class hierarchy which conceptually looks somewhat like this: #include <iostream> #include <stdexcept> class ExceptionBase : public std::runtime_error { public: ExceptionBase( const char * msg ) : std::runtime_error(msg) {} }; class OperationFailure : virtual public ExceptionBase { public: using ExceptionBase::ExceptionBase; }; class FileDoesNotExistError : virtual public ExceptionBase { public: using ExceptionBase::ExceptionBase; }; class

Fixing C++ Multiple Inheritance Ambiguous Call

試著忘記壹切 提交于 2019-12-22 05:38:28
问题 I have three classes structured like this: #include <iostream> using namespace std; class Keyword { public: virtual float GetValue() = 0; }; class CharacterKeyword : public Keyword { public: virtual float GetValue(){return _value;} private: float _value; }; class MeasurementKeyword : public Keyword { public: virtual float GetValue(){return _value;} private: float _value; }; class AddressType : public CharacterKeyword, public MeasurementKeyword { private: float address; float addresExt; }; int

C++ private virtual inheritance problem

℡╲_俬逩灬. 提交于 2019-12-22 01:26:48
问题 In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work? class A {}; class B: private virtual A {}; class C: public B {}; int main() { C c; return 0; } Moreover, if I remove the default constructor from A, e.g. class A { public: A(int) {} }; class B: private virtual A { public: B() : A(3) {} }; then class C: public B {}; would (unexpectedly) compile, but class

Why is single virtual inheritance not enough to resolve the dreaded diamond problem?

只谈情不闲聊 提交于 2019-12-21 17:45:14
问题 struct B { int i; }; struct D1 : virtual B {}; struct D2 : B {}; // <-- not virtual struct DD : D1, D2 {}; Having coded above, still the compiler demands D2 also to be virtual : DD d; d.i = 0; // error: request for member `i' is ambiguous What I don't understand is, once you have prompted compiler that B is virtual with respect to DD (via D1 ) then why it still i is ambiguous ? (If my memory serves correct, the older VC++ (in 2006), was capable enough to make out this just with single virtual

Virtual inheritance and empty vtable in base class

梦想的初衷 提交于 2019-12-21 06:09:30
问题 There is this code: #include <iostream> class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows: Derived::ptr to empty vtable Derived::y Base::x and it is 12 bytes. The question is - what is purpose of this empty vtable if there are not any virtual methods and

Static Virtual functions in c++

只愿长相守 提交于 2019-12-21 03:43:20
问题 I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static. How can I do that? 回答1: The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg: template< class DerivedType > class Base { public: static void

is virtual inheritance from pure abstract classes (interfaces) necessary

▼魔方 西西 提交于 2019-12-21 03:37:35
问题 Why is it that in the code below the compiler complains that PureAbstractBase is an ambiguous base class of MultiplyInheritedClass ? I realize I have two copies of the PureAbstractBase in MultiplyInheritedClass and that FirstConreteClass and SecondConreteClass should be derived virtually because they're the middle row of the diamond (and that does indeed fix the problem with the code below). But even though I have two copies of the interface why is it that the code in MultiplyInheritedClass

understanding vptr in multiple inheritance?

大兔子大兔子 提交于 2019-12-20 08:38:56
问题 I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in the above diagram is that there are only three vptrs even though four classes are involved. Implementations are free to generate four vptrs if they like, but three suffice (it turns out that B and D can share a vptr), and most implementations take