virtual inheritance

Discussion in 'C++' started by sksjava, Feb 11, 2004.

  1. sksjava

    sksjava Guest

    #include <iostream>
    struct parent {
    parent() : integer(0) { cout << "parent default\n"; }
    parent(int i) : integer(i) { cout << "parent non-default\n"; }
    int integer;
    };

    struct child1 : public parent {
    child1() { cout << "child1 default\n"; }
    child1(int i) : parent(i) { cout << "child1 non-default\n"; }
    };

    struct gchild : public child1 {
    gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
    };

    int main() {
    gchild g(10);
    cout << g.integer << endl;
    }


    The above code prints:
    parent non-default
    child1 non-default
    gchild non-default
    10


    Modifying the line "struct child1 : public parent" to "struct child1 :
    public virtual parent" will give the following result:
    parent default
    child1 non-default
    gchild non-default
    0


    I am totally confused on what the compiler is doing. Thanks in advance.
     
    sksjava, Feb 11, 2004
    #1
    1. Advertising

  2. sksjava wrote:
    > #include <iostream>


    using std::cout;
    using std::endl;

    > struct parent {
    > parent() : integer(0) { cout << "parent default\n"; }
    > parent(int i) : integer(i) { cout << "parent non-default\n"; }
    > int integer;
    > };
    >
    > struct child1 : public parent {
    > child1() { cout << "child1 default\n"; }
    > child1(int i) : parent(i) { cout << "child1 non-default\n"; }
    > };
    >
    > struct gchild : public child1 {
    > gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
    > };
    >
    > int main() {
    > gchild g(10);
    > cout << g.integer << endl;
    > }
    >
    >
    > The above code prints:
    > parent non-default
    > child1 non-default
    > gchild non-default
    > 10
    >
    >
    > Modifying the line "struct child1 : public parent" to "struct child1 :
    > public virtual parent" will give the following result:
    > parent default
    > child1 non-default
    > gchild non-default
    > 0
    >
    >
    > I am totally confused on what the compiler is doing. Thanks in advance.
    >

    In virtual inheritence the most derived type constructs the virtually
    inherited object(s). Therefore the following should print the required
    result:
    struct gchild : public child1 {
    gchild(int i) : child1(i), parent(10)
    {
    cout << "gchild non-default\n";
    }
    };
    If you had removed the parent's default ctor it wouldn't have compiled.

    Take the following example:
    struct child1 : virtual public parent {
    child1() : parent(20) { cout << "child1 default\n"; }
    };

    struct child2 : virtual public parent {
    child2() : parent(10) { cout << "child2 default\n"; }
    };

    struct gchild : public child1, public child2 {
    gchild() { cout << "gchild non-default\n"; }
    };
    what value should parent.integer be? Hence you have to call an
    appropriate parent ctor in all the derived types.

    Mike
     
    Michael Mellor, Feb 11, 2004
    #2
    1. Advertising

  3. sksjava

    buildsoc Guest

    i think that the compiler produced some code to invoke parent's default
    constructor( parent() ) that locate the position of parent subject in the
    gchild object.
    these codes were put into both of child constructors and do some job like
    landing party.
    so the parent(int i) was not invoked.


    ~ Let us linux ~


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
     
    buildsoc, Feb 12, 2004
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. qazmlp
    Replies:
    19
    Views:
    824
    Daniel T.
    Feb 4, 2004
  3. cppsks
    Replies:
    0
    Views:
    853
    cppsks
    Oct 27, 2004
  4. Ashwin
    Replies:
    2
    Views:
    367
    Pierre Barbier de Reuille
    Aug 1, 2006
  5. Replies:
    1
    Views:
    482
    Alf P. Steinbach
    Mar 14, 2009
Loading...

Share This Page