Hello all, In a large project I'm participating in, a design dilemma have arrived. Adding virtual inheritance would solve it beautifully on the source code level, but a potential drawback is the speed penalty it brings. Measurement is always the way of approaching performance questions, but since that sometimes can be tricky, I thought of starting with getting a theoretical understanding of virtual inheritance.
Consider this code: //---------------------------------------------- class Shared { public: Shared() : m_ref(0) { } virtual ~Shared() { } int refCount() const { return m_ref; } void ref() { m_ref++; } void deref() { if(m_ref) m_ref--; if(!m_ref) delete this; } protected: unsigned int m_ref; }; class B : public virtual Shared {}; class C : public virtual Shared {}; class D : public Shared {}; class BC : public B, public C {}; class DerivedB : public B {}; //---------------------------------------------- Follows do plenty of questions: * Is class DerivedB in anyway affected by that its sub class B is a public virtual of Shared? And if so, how? * What are the differences in memory usage between an instance of class D and an instance of class B? (if any) * What are the performance differences of invoking BCinstance->ref() compared to Dinstance->ref()? * What are the performance differences in constructing and destructing an instance of class BC and D? Speaking in general terms, what are the penalties of virtual inheritance? Consider that a class previously was as class D, and then became class BC. How would that affect performance? Hard to tell? Definitely a performance disaster? Or perhaps that "the additional level of indirection has a slight performance overhead, but it's a small price to pay"?[1] Detailed replies, pointers to documentation, is much appreciated. A googling showed relatively contradictory results on the performance of virtual inheritance, so solid information would be a relief(explanations to why it is as it is would also be interesting to hear). Thanks in advance, Frans 1. http://www.devx.com/tips/Tip/12789