virtual-inheritance

Is this use pattern of virtual inheritance for “method injection” a known paradigm?

廉价感情. 提交于 2019-12-10 13:39:23
问题 Yesterday, I came across this question: forcing unqualified names to be dependent values Originally, it seemed like a very specific question related to broken VC++ behaviour, but while trying to solve it, I stumbled upon a use pattern of virtual inheritance I hadn't come across before (I will explain it in a second, after telling you the question I have). I found it interesting, so I looked for it on SO and google, but I couldn't find anything. Maybe, I just don't know the right name for it (

Should you write “public virtual” or “virtual public” in virtual inheritance?

旧时模样 提交于 2019-12-10 13:04:00
问题 Based on http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually inheriting Animal: class Mammal : public virtual Animal { ... }; I also saw books use the following syntax, class Mammal : virtual public Animal { ... }; Question> Which is one the C++ standard? Thank you 回答1: From ISO/IEC 14882:2003(E) - 10.1 A list of base classes can be specified in a class definition using the notation: base-clause: : base-specifier-list base-specifier-list: base

Why does virtual inheritance need a vtable even if no virtual functions are involved?

我们两清 提交于 2019-12-10 12:48:01
问题 I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which tells us: However this offset can in the general case only be known at runtime,... I don't get what is runtime-related here. The complete class inheritance hierarchy is already known at compile time. I understand virtual functions and the use of a

Why does virtual inheritance need to be specified in the middle of a diamond hierarchy?

自古美人都是妖i 提交于 2019-12-09 15:28:39
问题 I have diamond hierarchy of classes: A / \ B C \ / D To avoid two copies of A in D, we need to use virtual inheritance at B and C. class A { }; class B: virtual public A {}; class C: virtual public A { }; class D: public B, public C { }; Question: Why does virtual inheritance needs to be performed at B and C, even though the ambiguity is at D? It would have been more intuitive if it is at D. Why is this feature designed like this by standards committee? What can we do if B and C classes are

Virtual Inheritance, one class enough?

只愿长相守 提交于 2019-12-09 15:21:58
问题 I understand the concept of virtual inheritance, but I couldn't find the answer to this anywhere. Say you have class D which inherits class B and C. Both B and C inherit class A. So you could make B and C virtually inherit A to avoid two instances of A. But do you have to specify virtual inheritance at both B and C or does it already create only one instance of A if one of the two virtually inherits A and the other doesn't? Thanks 回答1: They must all be virtual . From C++11 10.1 [class.mi]/7:

virtual base offset in virtual function table for virtual inheritance

被刻印的时光 ゝ 提交于 2019-12-08 21:56:09
问题 The codes are as follows (C++11 codes compiled with G++-5.4 on Ubuntu 16.04): #include <iostream> using namespace std; class Base { public: virtual void show() { cout << "Base" << endl; } virtual void func() { cout << "func in Base" << endl; } protected: int base = 15; }; class A: public virtual Base { public: void show() override { cout << this << endl; cout << 'A' << endl; } void func() override { cout << this << endl; cout << "func in A" << endl; } protected: int a = 31; }; int main(int

multiple inheritance without virtual inheritance

陌路散爱 提交于 2019-12-08 15:24:29
问题 I am trying to understand multiple inheritance, here is my code: struct A { A() {} static int n; static int increment() { return ++n; } }; int A::n = 0; struct B : public A {}; struct C : public A {}; struct D : public B, C {}; int main() { D d; cout<<d.increment()<<endl; cout<<d.increment()<<endl; } This code works. However, if I change increment() to non-static it will fail. My questions: Why compiler complains ambiguous call of non-static version of increment() , while satisfies with the

What is the proper approach to swap and copy idiom in virtual inheritance?

妖精的绣舞 提交于 2019-12-07 07:32:41
问题 Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class.

How to downcast from non-polymorphic virtual base class?

坚强是说给别人听的谎言 提交于 2019-12-07 05:21:45
问题 Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived &md = somehow_cast<MostDerived&>(v); //but HOW? md.ok = true; } int main() { MostDerived md; foo(md); } Please note that the

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