virtual-inheritance

Performance impact of virtual inheritance

醉酒当歌 提交于 2019-11-29 05:27:46
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. Common implementations will make access to data members of virtual base classes use an additional indirection. As James points out in his comments, calling a member function of a base class in a multiple inheritance scenario

Virtual inheritance doesn't break static composition?

给你一囗甜甜゛ 提交于 2019-11-29 00:25:02
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? Data Layout in non-virtual Inheritance: class Point2d { int x_, y_; }; class Point3d : public Point2d { int z_; }; Point2d: +--------------+ | int x_ | +--------------+ | int y_ | +--------------+ Point3d: +--------------+ --+ | int x_ | | +--------------+ +-- Point2d subobject | int y_ | | +--------------+ --+ | int z_ | +---------

Dominance in virtual inheritance

拜拜、爱过 提交于 2019-11-28 20:44:53
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. 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 lookup in a class scope, C. First, every declaration for the name in the class and in each of its base class

Eliminate duplicate entries from C++11 variadic template arguments

限于喜欢 提交于 2019-11-28 19:46:15
I'm using variadic templates with multiple virtual inheritance in C++ to aggregate types into a single structure definition. Here is a sample set of structures: struct meas { int i; }; struct meas2 : public virtual meas { int j; }; struct meas3 : public virtual meas { int k; }; I then aggregate these using multiple virtual inheritance: template <typename... Args> struct zipper : public virtual Args... {}; I can then do: typedef zipper<meas, meas2> meas_type; meas* m = new meas_type; These can then cascade: typedef zipper<meas3, meas_type> meas_type2; The resulting object, however, is rather

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

*爱你&永不变心* 提交于 2019-11-28 19:40:54
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 members, and the object to which the call applies is the object (call it x) under construction or

C++ Inheritance via dominance warning

孤者浪人 提交于 2019-11-28 18:10:29
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 this entire tree only D implements virtual methods, there is no other definition of the method in question

In C++, should I almost always use virtual inheritance?

百般思念 提交于 2019-11-28 09:39:38
I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and conventional inheritance only when needed? It seems like it'd lead to more future-proof class design, but maybe I'm missing some pitfall. The drawbacks are that All classes will have to initialize all its virtual bases all the time (e.g. if A is virtual base of B, and C derives from B, it also have to initialize A itself). You have to use more expensive dynamic_cast everywhere you use a static_cast

Mixing virtual and non-virtual inheritance of a base class

强颜欢笑 提交于 2019-11-28 08:57:56
This is the code: struct Biology { Biology() { cout << "Biology CTOR" << endl; } }; struct Human : Biology { Human() { cout << "Human CTOR" << endl; } }; struct Animal : virtual Biology { Animal() { cout << "Animal CTOR" << endl; } }; struct Centaur : Human, Animal { Centaur() { cout << "Centaur CTOR" << endl; } }; int main() { Centaur c; return 0; } This code prints: Biology CTOR Biology CTOR Human CTOR Animal CTOR Centaur CTOR Why? Since we create a Centaur object, we start from building the Centaur by constructing Human , Animal and finally Centaur (we start from the less derived to the

Why does virtual keyword increase the size of derived a class?

北城以北 提交于 2019-11-28 08:35:13
问题 I have two classes - one base class and one derived from it : class base { int i ; public : virtual ~ base () { } }; class derived : virtual public base { int j ; }; main() { cout << sizeof ( derived ) ; } Here the answer is 16. But if I do instead a non-virtual public inheritance or make the base class non-polymorphic , then I get the answer as 12, i.e. if I do : class base { int i ; public : virtual ~ base () { } }; class derived : public base { int j ; }; main() { cout << sizeof ( derived

Is there any penalty/cost of virtual inheritance in C++, when calling non-virtual base method?

房东的猫 提交于 2019-11-28 02:29:18
问题 Does using virtual inheritance in C++ have a runtime penalty in compiled code, when we call a regular function member from its base class? Sample code: class A { public: void foo(void) {} }; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {}; // ... D bar; bar.foo (); 回答1: There may be, yes, if you call the member function via a pointer or reference and the compiler can't determine with absolute certainty what type of object that pointer or reference