virtual-inheritance

Virtual multiple inheritance - final overrider

时光总嘲笑我的痴心妄想 提交于 2019-11-28 01:58:33
问题 while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example: #include<iostream> using namespace std; class Base { public: virtual void f(){ cout << "Base.f" << endl; } }; class Left : public virtual Base { }; class Right : public virtual Base{ public: virtual void f(){ cout << "Right.f" << endl; } }; class Bottom : public Left, public Right{ }; int main(int argc,char **argv) { Bottom* b = new Bottom(); b->f(); } The above, somehow, compiles and

Virtual inheritance in C++

隐身守侯 提交于 2019-11-27 23:38:51
I found this in a website while reading about virtual inheritance in c++ When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy: //Note: this is a simplified description of iostream classes class ostream: virtual public ios { /*..*/ } class istream: virtual public ios { /*..*/ } class iostream : public istream, public ostream { /*..*/ } //a single ios inherited How does C++ ensure that only a single instance of a virtual member exists, regardless of the number of classes derived from it? C++ uses

Performance impact of virtual inheritance

本秂侑毒 提交于 2019-11-27 23:00:33
问题 I am considering using virtual inheritance in a real-time application. Does using virtual inheritance have a performance impact similar to that of calling a virtual function? The objects in question would only be created at start up but I'm concerned if all functions from the hierarchy would be dispatched via a vtable or if only those from the virtual base class would be. 回答1: Common implementations will make access to data members of virtual base classes use an additional indirection. As

Virtual Inheritance: Error: no unique final overrider

非 Y 不嫁゛ 提交于 2019-11-27 20:11:32
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 follows: Base /\ / \ Der1 Der2 \ / Der3 I know about the dreadful diamond on derivation issue and that's

C++ cannot convert from base A to derived type B via virtual base A

笑着哭i 提交于 2019-11-27 17:57:46
I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual base A In order to understand the cast system you need to dive in the object model. The classic representation of a simple hierarchy model is containment: that if B derives from A then the B object will in fact contain a A subobject alongside its own attributes. With this model, downcasting is a simple pointer manipulation, by an offset known at

Virtual inheritance doesn't break static composition?

左心房为你撑大大i 提交于 2019-11-27 15:38:04
问题 I was working the last 5 years with the assumption that virtual inheritance breaks static composition. But now I discovered, that static composition is still maintained, there is just additional information about the location of the correct instance. Is this right? 回答1: Data Layout in non-virtual Inheritance: class Point2d { int x_, y_; }; class Point3d : public Point2d { int z_; }; Point2d: +--------------+ | int x_ | +--------------+ | int y_ | +--------------+ Point3d: +--------------+ --+

Dominance in virtual inheritance

梦想与她 提交于 2019-11-27 13:07:52
问题 What are the C++98/C++03 standards' and the C++0x future standard's exact rules for dominance in virtual inheritance? I'm not asking for just the specific paragraphs, although I'm asking also for that (somewhere in section 10, I'd guess). I'm asking also for the consequences of the standardese, the standardese explained, clearly. 回答1: I think that this is the language you're looking for. In the C++03 ISO spec, in §10.2/2, we have the following: The following steps define the result of name

C++ constructors: why is this virtual function call not safe?

馋奶兔 提交于 2019-11-27 12:28:30
问题 This is from the C++11 standard sec 12.7.4. This is rather confusing. What does the last sentence in the text mean exactly? Why is the last method call in B::B undefined? Shoudn't it just call a.A::f ? 4 Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data

C++ Inheritance via dominance warning

巧了我就是萌 提交于 2019-11-27 11:19:42
问题 I'm trying to implement a rather large object that implements many interfaces. Some of these interfaces are pure virtual. I may have a problem in diamond inheritance. Visual Studio is reporting a warning of C4250 ('class1' : inherits 'class2::member' via dominance) . First of all these classes are inherited virtually as it should be. The following is the partial class design that causes this problem. A B C \ / \ / \ / \ / AB BC | | | BC2 | | \ D: Implementation of B, C, BC, BC2 \ / Big In

Why must virtual base classes be constructed by the most derived class?

痞子三分冷 提交于 2019-11-27 09:12:56
The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A 's constructor in C 's constructor initialization list, that is: // most derived class class C: public B { public: C(): A(0) {} // OK!!! }; it does work. Apparently, the reason is because virtual base classes must always be constructed by the most derived classes . I don't understand the reason behind this limitation. Caleth Because it avoids this: class A { public: A(int) {} }; class B0: virtual