-4

Possible Duplicate:
In C++, what is a virtual base class?
virtual inheritance

why is it so that the constructors of virtual base classes are called from most derived class...and in the inheritance hierarchy first the objects to virtual base classes are created...whats the logic behind this?

I understand that using virtual inheritance as in diamond structure using virtual inheritance only one copy of the most base class is created but what exactly is virtual doing in a linear inheritance.

class A{};
class B : virtual public A {};  
class C : virtual public B {};

What actually we try to achieve on using this kind of inheritance?

Also what is the object layout in case of virtual inheritance ?

Can someone explain the logic behind this kind of behaviour in C++?

Community
  • 1
  • 1
Kunal
  • 511
  • 2
  • 6
  • 12
  • 4
    This is your 12th question, you should know how to use proper formatting in a question until now. – penelope May 10 '12 at 14:25
  • 1
    The question isn't clear. Are you asking why A's constructor is called first? – XTF May 10 '12 at 14:27
  • 2
    You're basically asking for a tutorial on virtual inheritance. This has been covered in many other questions, and several online tutorials. SO is not your Google. – Mike DeSimone May 10 '12 at 14:28
  • 1
    Kunal: I didn't downvote you, but I would venture a guess why your getting downvoted here. 1) There are way too many questions here. 2) This looks like a stream of consciousness and [Stack Overflow is not your personal research assistant](http://meta.stackexchange.com/a/128553/142865), 3) You are using virtual inheritance and asking why use virtual inheritance. If you don't know, they why are you using it? (Basically the same as #2) 4) You are not showing any effort to format your questions correctly -- other users are having to repeatedly edit your posts just to make them readable. – John Dibling May 10 '12 at 14:34
  • 1
    @penelope: people always do it for him, why should he bother? – PlasmaHH May 10 '12 at 14:45

1 Answers1

2

Virtual inheritance should arguably be the default. If I write something like:

class Base
{
public:
    virtual ~Base() {}
    virtual void f() = 0;
    virtual int g() const = 0;
    //  ...
};

, it is clear that this class is meant to be inherited from. If I later write:

class Middle : public virtual Base
{
public:
    virtual void f();  
};

, the class is still clearly meant to be a base class—it has only implemented part of the interface. In this case, the inheritance should be virtual, since I don't know (or don't want to impose just one solution) whether the implementation of g() will be in a further derived class, or in a sister class (mixin-technology). Thus,

class Derived1 : public Middle
{
public:
    virtual int g() const;
};

No diamand, but the author of Middle didn't know that this would be the case, and didn't want to forbid:

class M2 : public virtual Base
{
public:
    virtual int g() const;
};

class Derived2 : public Middle, public M2
{
};

And given such a hierarchy, who should call Base's constructor. The only reasonable candidate is Derived2.

James Kanze
  • 150,581
  • 18
  • 184
  • 329