469,638 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,638 developers. It's quick & easy.

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 2606
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by JKop | last post: by
3 posts views Thread by cyrusNew | last post: by
5 posts views Thread by Tony Johansson | last post: by
3 posts views Thread by marv | last post: by
13 posts views Thread by John Salerno | last post: by
14 posts views Thread by Bruno van Dooren | last post: by
8 posts views Thread by ^MisterJingo^ | last post: by
20 posts views Thread by Daniel | last post: by
7 posts views Thread by Markus Svilans | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.