Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization of virtual bases

Reply
Thread Tools

Initialization of virtual bases

 
 
Dave Theese
Guest
Posts: n/a
 
      08-24-2003
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;
}



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math problem - converting between arbitrary bases in perl - help! ! aaa Perl 1 05-28-2004 01:42 AM
High performance alternative to MI of virtual bases christopher diggins C++ 31 04-09-2004 08:10 PM
Help with data bases news HTML 4 11-14-2003 01:44 PM
All Your Bases Are Belong To Us Zombie Computer Support 0 08-31-2003 01:20 PM
virtual methods, virtual bases bug<?> in G++ 3.3 Jim Fischer C++ 3 07-31-2003 08:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57