Initialization of virtual bases

Discussion in 'C++' started by Dave Theese, Aug 24, 2003.

  1. Dave Theese

    Dave Theese Guest

    Hello all,

    The code example below has proper behavior (of course), but I'm trying to
    understand how the behavior is brought about. Specifically, what happens to
    B's and C's initialization of A??? Is it just ignored even though it
    explicitly appears in the code?

    Thanks,
    Dave

    #include <iostream>

    using namespace std;

    class A
    {
    public:
    A(int d): data(d)
    {
    cout << "data initialized to " << d << endl;
    }

    private:
    int data;
    };

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

    class C: virtual public A
    {
    public:
    C(int d): A(d)
    {
    }
    };

    class D: public B, public C
    {
    public:
    D(int d): A(d), B(d + 1), C(d + 2)
    {
    }
    };

    int main(void)
    {
    // Displays "data initialized to 1"
    D foo(1);

    return 0;
    }
     
    Dave Theese, Aug 24, 2003
    #1
    1. Advertising

  2. "Dave Theese" <> wrote in message
    news:QSX1b.7781$QT5.1646@fed1read02...
    > Hello all,
    >
    > The code example below has proper behavior (of course), but I'm trying to
    > understand how the behavior is brought about. Specifically, what happens

    to
    > B's and C's initialization of A??? Is it just ignored even though it
    > explicitly appears in the code?
    >
    > Thanks,
    > Dave
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class A
    > {
    > public:
    > A(int d): data(d)
    > {
    > cout << "data initialized to " << d << endl;
    > }
    >
    > private:
    > int data;
    > };
    >
    > class B: virtual public A
    > {
    > public:
    > B(int d): A(d)
    > {
    > }
    > };
    >
    > class C: virtual public A
    > {
    > public:
    > C(int d): A(d)
    > {
    > }
    > };
    >
    > class D: public B, public C
    > {
    > public:
    > D(int d): A(d), B(d + 1), C(d + 2)
    > {
    > }
    > };
    >
    > int main(void)
    > {
    > // Displays "data initialized to 1"
    > D foo(1);
    >
    > return 0;
    > }
    >


    Yes I believe so, virtual base classes are initialised by the more derived
    object (or something like that). So if you created a B or a C object then B
    or C would be responsible for initialising A.

    john
     
    John Harrison, Aug 24, 2003
    #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. ! aaa
    Replies:
    1
    Views:
    1,101
    ! aaa
    May 28, 2004
  2. news

    Help with data bases

    news, Nov 14, 2003, in forum: HTML
    Replies:
    4
    Views:
    443
  3. Jim Fischer
    Replies:
    3
    Views:
    453
    Jim Fischer
    Jul 31, 2003
  4. Dave

    Private bases

    Dave, Nov 18, 2003, in forum: C++
    Replies:
    8
    Views:
    482
    Mike Wahler
    Nov 18, 2003
  5. christopher diggins
    Replies:
    31
    Views:
    1,027
    Mark A. Gibbs
    Apr 9, 2004
Loading...

Share This Page