Question about virtual inheritance

Discussion in 'C++' started by lok, Nov 17, 2003.

  1. lok

    lok Guest

    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() ?
    lok, Nov 17, 2003
    #1
    1. Advertising

  2. lok

    tom_usenet Guest

    On 16 Nov 2003 22:21:47 -0800, (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
    tom_usenet, Nov 17, 2003
    #2
    1. Advertising

  3. lok

    tom_usenet Guest

    On Mon, 17 Nov 2003 13:27:16 +0000, tom_usenet
    <> wrote:

    >KeyInfo, then PairInfo.


    I meant AlgInfo rather than PairInfo of course.

    Tom
    tom_usenet, Nov 17, 2003
    #3
    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. qazmlp
    Replies:
    19
    Views:
    766
    Daniel T.
    Feb 4, 2004
  2. Squeamizh
    Replies:
    2
    Views:
    266
    Squeamizh
    Jun 8, 2004
  3. cppsks
    Replies:
    0
    Views:
    797
    cppsks
    Oct 27, 2004
  4. Ashwin
    Replies:
    2
    Views:
    332
    Pierre Barbier de Reuille
    Aug 1, 2006
  5. Replies:
    1
    Views:
    451
    Alf P. Steinbach
    Mar 14, 2009
Loading...

Share This Page