virtual-inheritance

Where is the “virtual” keyword necessary in a complex multiple inheritance hierarchy?

为君一笑 提交于 2019-12-20 08:33:07
问题 I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes: A / \ B C / \ / \ D E F \ / \ / G H \ / I If I want to ensure that none of the classes appear more than once in any of the subclasses, which base classes need to be marked virtual ? All of them? Or is it sufficient to use it only on those classes that derive directly from a class that

Force deriving from a class virtually

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:56:41
问题 We have a special framework for interfaces in our project, and part of the requirements is that classes which represent an interface may only be used as virtual base classes, not as non-virtual ones. Is there a way to enforce this in code? That is, produce a compilation error if the class is derived from non-virtually. I have access to C++11 as implemented by VS 2010: this means static_assert , enable_if and <type_traits> are available. 回答1: IMO, there is no clean and platform independent

Calling a virtual base class's overloaded constructor

随声附和 提交于 2019-12-19 05:44:31
问题 Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example: class A { const int i; public: A() : i(0) { cout << "calling A()" << endl; } A(int p) : i(p) { cout << "calling A(int)" << endl; } }; class B : public virtual A { public: B(int i) : A(i) { cout << "calling B(int)" << endl; } }; class C : public B { public: C(int i) : A(i), B(i) { cout << "calling C(int)" << endl; } }; class D : public C { public: D(int i) : /*A(i), */ C(i) { cout << "calling D(int)"

C++ virtual inheritance initializer list

故事扮演 提交于 2019-12-19 01:37:25
问题 in the following code: class A { public: int x; A(int x):x(x){} }; class B: public virtual A { public: B(int x):A(x){} }; class C: public virtual A { public: C(int x):A(x){} }; class D: public B, public C { public: D(int x):B(x++), C(x++), A(x++){} }; two questions: Why do I need to add A(...) in D's initializer list? D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x , why? 回答1: Why do I need to add A(...) in D's initializer

Class sizes with virtual inheritance in C++

假装没事ソ 提交于 2019-12-18 17:05:04
问题 #include<iostream> using namespace std; class abc { int a; }; class xyz : public virtual abc { int b; }; int main() { abc obj; xyz obj1; cout<<endl<<sizeof(obj); cout<<endl<<sizeof(obj1); return 0; } The answers would be compiler dependent but I'm surprized when I saw this as the result ~/Documents/workspace/tmp ‹.rvm-› $ ./class_sizes 4 16 If I remove the virtual keyword then the size allocated is 4 and 8 respectively which is what I expected. Why is the extra space being taken up exactly? I

Class sizes with virtual inheritance in C++

百般思念 提交于 2019-12-18 17:04:13
问题 #include<iostream> using namespace std; class abc { int a; }; class xyz : public virtual abc { int b; }; int main() { abc obj; xyz obj1; cout<<endl<<sizeof(obj); cout<<endl<<sizeof(obj1); return 0; } The answers would be compiler dependent but I'm surprized when I saw this as the result ~/Documents/workspace/tmp ‹.rvm-› $ ./class_sizes 4 16 If I remove the virtual keyword then the size allocated is 4 and 8 respectively which is what I expected. Why is the extra space being taken up exactly? I

Method resolution order in C++

孤街醉人 提交于 2019-12-17 18:56:57
问题 Consider the following class hierarchy: base class Object with a virtual method foo() an arbitrary hierarchy with multiple inheritance (virtual and non-virtual); each class is a subtype of Object; some of them override foo(), some don't a class X from this hierarchy, not overriding foo() How to determine which method will be executed upon a call of foo() on an object of class X in C++? (I'm looking for the algorithm, not any specific case.) 回答1: There is no MRO in C++ like Python. If a method

Is Virtual Inheritance necessary for Exceptions?

喜你入骨 提交于 2019-12-17 16:44:51
问题 I understand the need for virtual inheritance when using multiple inheritance -- it solves the Dreaded Diamond Problem. But what if I'm not using multiple inheritance? Is there a need for virtual inheritance at all? I seem to recall hearing that it was important for exceptions (throw a derived class, catch by base class reference). But shouldn't virtual destructors be sufficient for that? I've tried searching for the reference page I once saw on this, but I can't seem to find it. 回答1: You're

Virtual Inheritance: Error: no unique final overrider

孤街浪徒 提交于 2019-12-17 15:52:13
问题 I know virtual inheritance is covered here before and before asking this question I went through the detail of the virtual inheritance and went through the details of a similar problem like the followings: multiple-diamond-inheritance-compiles-without-virtual-but-doesnt-with and why does gcc give me an error - final overrider My problem is slightly different as I am not using pure virtual function and explicitly using virtual inheritance to have one unique 'Base' class. The hierarchy is as

When virtual inheritance IS a good design?

最后都变了- 提交于 2019-12-17 08:32:20
问题 EDIT3: Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the question (I know that's also my fault, sorry for that) Hi, I've looked over the questions on virtual inheritance ( class B: public virtual A {...} ) in C++, but did not find an answer to my question. I know that there are some issues with virtual inheritance, but what I'd like to know is in