virtual base class and friend

Discussion in 'C++' started by omveer.chaudhary@gmail.com, Feb 28, 2013.

  1. Guest

    Hi,

    I'm trying below scenario:
    class xx
    {
    xx(){}
    friend class yy;
    };

    class yy: virtual public xx
    {
    public:
    yy(){}
    };

    class zz:public yy
    {
    public:
    };

    Q1: It doesn't work with ZZ obj; Please help me understanding it.

    class xx
    {
    xx(){}
    friend class yy;
    };

    class yy: public xx
    {
    public:
    yy(){}
    };

    class zz:public yy
    {
    public:
    };

    Q2: After removing virtual, it works for zz obj;. Why and how?

    Thanks in advance.

    Regards
    , Feb 28, 2013
    #1
    1. Advertising

  2. <> wrote:
    > Hi,
    >
    > I'm trying below scenario:
    > class xx
    > {
    > xx(){}
    > friend class yy;
    > };
    >
    > class yy: virtual public xx
    > {
    > public:
    > yy(){}
    > };
    >
    > class zz:public yy
    > {
    > public:
    > };
    >
    > Q1: It doesn't work with ZZ obj; Please help me understanding it.


    Your class zz implicitly has the following constructor:

    class zz : public yy
    {
    public:
    zz() : xx(), yy() {}
    }

    Constuctors of virtual base classes are called from every derived class
    directly.

    Consider the following case:

    class A
    {
    public:
    A();
    A(int i);
    };

    class B : virtual public A
    {
    public:
    B() : A() {}
    };

    class C : virtual public A
    {
    public:
    C() : A(4) {}
    };

    class D : public B, public C
    {
    public:
    // D() : B(), C() {} // -> Error, the A subobject
    // is the same for B and C
    // and can only be
    // constructed once. But use
    // A() or A(4)?
    D() : A(), B(), C() {} // Correct
    };

    > class xx
    > {
    > xx(){}
    > friend class yy;
    > };
    >
    > class yy: public xx
    > {
    > public:
    > yy(){}
    > };
    >
    > class zz:public yy
    > {
    > public:
    > };
    >
    > Q2: After removing virtual, it works for zz obj;. Why and how?

    Your class zz implicitly has the following constructor:

    class zz : public yy
    {
    public:
    zz() : yy() {}
    }

    Constuctors of non-virtual base are only called from classes that inherit
    directly.

    Consider again the same case, but without virtual inheritance:

    class A
    {
    public:
    A();
    A(int i);
    };

    class B : public A
    {
    public:
    B() : A() {}
    };

    class C : public A
    {
    public:
    C() : A(4) {}
    };

    class D : public B, public C
    {
    public:
    D() : B(), C() {} // Correct: B and C have both
    // their own A object and
    // do the initialization on their own.
    // D() : A(), B(), C() {} // Error
    };

    Tobi
    Tobias Müller, Feb 28, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tomek
    Replies:
    2
    Views:
    664
    Andrey Tarasevich
    Dec 1, 2003
  2. tomek
    Replies:
    3
    Views:
    732
    David Fisher
    Nov 30, 2003
  3. Chunhui Han
    Replies:
    2
    Views:
    491
  4. Replies:
    7
    Views:
    368
    Richard
    Aug 28, 2006
  5. Peter
    Replies:
    2
    Views:
    265
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page