473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Initialization of virtual bases

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;
}

Jul 19 '05 #1
1 1344

"Dave Theese" <ch**********@yahoo.com> 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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
1
by: Alexander Stippler | last post by:
Hello, I want to build up an inheritance structure looking like two coupled dreaded diamonds. It's similar to an old posting of me. Now my question is, where to use the keyword virtual. CV /...
9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
2
by: lok | last post by:
consider follow simple code: class Base { protected: int m_nAddr; public: Base(int addr_): m_nAddr(addr_) {} virtual ~Base() {} virtual int GetData() const = 0; };
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
27
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting...
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
12
by: Christoph Zwerschke | last post by:
Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.