virtual inheritance.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Radde

    virtual inheritance.

    Hello,
    when i run the below code.. I get output

    A()
    A()
    B()
    C()
    D()

    dont know? why in this sequence?

    #include<iostre am.h>

    class A
    {
    public:
    A()
    {
    cout<<"A()"<<en dl;
    }
    };

    class B: public A
    {
    public:
    B()
    {
    cout<<"B()"<<en dl;
    }
    };
    class C: public virtual A
    {
    public:
    C()
    {
    cout<<"C()"<<en dl;
    }
    };

    class D: public B,public C
    {
    public:
    D()
    {
    cout<<"D()"<<en dl;
    }
    };

    void main()
    {
    D d;
    }

    But when i make class B : public virtual A{}
    and class C : public A(}

    i get output
    A()
    B()
    A()
    C()
    D()

    how is this??

    I get same output even if i dont use virtual inheritance(sam e as
    above)..This is agreeable..But i dont know the first one and second
    one?? I realy didnt understand the constructor calling mechanism..

    Cheers..

  • Victor Bazarov

    #2
    Re: virtual inheritance.

    Radde wrote:[color=blue]
    > [...initialisati on of virtual base subobject seems out of order...]
    > how is this??[/color]

    All you need to know is that virtual base subobjects are constructed by
    the most derived class' constructor's initialiser list.
    [color=blue]
    > [...][/color]

    V

    Comment

    • Mark P

      #3
      Re: virtual inheritance.

      Radde wrote:[color=blue]
      > Hello,
      > when i run the below code.. I get output
      >
      > A()
      > A()
      > B()
      > C()
      > D()
      >
      > dont know? why in this sequence?
      >
      > #include<iostre am.h>
      >
      > class A
      > {
      > public:
      > A()
      > {
      > cout<<"A()"<<en dl;
      > }
      > };
      >
      > class B: public A
      > {
      > public:
      > B()
      > {
      > cout<<"B()"<<en dl;
      > }
      > };
      > class C: public virtual A
      > {
      > public:
      > C()
      > {
      > cout<<"C()"<<en dl;
      > }
      > };
      >
      > class D: public B,public C
      > {
      > public:
      > D()
      > {
      > cout<<"D()"<<en dl;
      > }
      > };
      >
      > void main()
      > {
      > D d;
      > }
      >[/color]

      Here's my understanding:

      First constructed is the virtual A, then D inititiates B and C, and B
      initiates A, so A is constructed again, followed by B, then C, and
      finally D.
      [color=blue]
      > But when i make class B : public virtual A{}
      > and class C : public A(}
      >
      > i get output
      > A()
      > B()
      > A()
      > C()
      > D()
      >
      > how is this??[/color]

      Again, first virtual A, then D initiates B and C, so B is constructed
      first then for C we get A and C in that order, and finally D.
      [color=blue]
      >
      > I get same output even if i dont use virtual inheritance(sam e as
      > above)..This is agreeable..But i dont know the first one and second
      > one?? I realy didnt understand the constructor calling mechanism..
      >
      > Cheers..
      >[/color]

      Basically the virtual constructors are called first and then what's left
      over is called in depth-first left-to-right order.

      Comment

      • leonardo77

        #4
        Re: virtual inheritance.

        All you need to know is that you should first read the C++-FAQ-Lite!
        :->)) Site:"parashift .com\c++-faq-lite" ,look for virtual inheritance.

        Isn't the FAQ supposed to handle cases like this?

        _______________ _______________ _______________ ____________

        leo77

        There is nothing new in this universe...

        Comment

        Working...