473,396 Members | 2,011 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,396 software developers and data experts.

Question about virtual inheritance

lok
consider follow simple code:

class Base {
protected:
int m_nAddr;
public:
Base(int addr_): m_nAddr(addr_) {}
virtual ~Base() {}
virtual int GetData() const = 0;
};

class KeyInfo: virtual public Base {
protected:
int m_nKey;
public:
KeyInfo(int addr_, int key_): Base(addr_), m_nKey(key_) {}

int GetData() const {
return m_nKey;
}
};

class AlgInfo: virtual public Base {
protected:
int m_nAlg;
public:
AlgInfo(int addr_, int alg_): Base(addr_), m_nAlg(alg) {}

void GetData() const {
return m_nAlg;
}
};

class PairInfo: public KeyInfo, public AlgInfo {
protected:
int m_nPair;
public:
PairInfo(int addr_, int key_, int alg_, int pair_)
: Base(addr_), KeyInfo(addr_, key_), AlgInfo(addr_, alg_),
m_nPair(pair_) {}

void GetData() const {
return m_nPair;
}
};
Q1. When an PairInfo is created, before its constructor PairInfo()
called, compiler will call Base(), KeyInfo() & AlgInfo, in which order
these 3 constructor will be called ?

Q2. As it is virtual inheritance, PairInfo only have a copy of Base,
so when constructor KeyInfo() and AlgInfo() are called, they will not
call their own parent class constructor Base() ?
Jul 19 '05 #1
2 2792
On 16 Nov 2003 22:21:47 -0800, ov****@sinatown.com (lok) wrote:

Example condensed to:

Base
/ \
KeyInfo AlgInfo
\ /
PairInfo

[SNIP]

Q1. When an PairInfo is created, before its constructor PairInfo()
called, compiler will call Base(), KeyInfo() & AlgInfo, in which order
these 3 constructor will be called ?
Virtual bases are initialized first, based on the order they appear in
the inheritence list. Then direct base classes are initialized in the
order you listed them in the base specifier list. So its Base, then
KeyInfo, then PairInfo.
Q2. As it is virtual inheritance, PairInfo only have a copy of Base,
so when constructor KeyInfo() and AlgInfo() are called, they will not
call their own parent class constructor Base() ?


No. Only the most derived sub object initializes virtual bases, hence
the Base parts of the initializer lists of KeyInfo and AlgInfo are
ignored when a PairInfo is constructed.

Tom
Jul 19 '05 #2
On Mon, 17 Nov 2003 13:27:16 +0000, tom_usenet
<to********@hotmail.com> wrote:
KeyInfo, then PairInfo.


I meant AlgInfo rather than PairInfo of course.

Tom
Jul 19 '05 #3

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

Similar topics

4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: cyrusNew | last post by:
Hi, sorry if it was already asked, I have a following class diagram A =? A | | B C \ /
5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
3
by: marv | last post by:
If I have abstract base classes like these: //--------- class IBase { public: virtual void Action(void) = 0; };
13
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.