Multiple inheritance enables a derived class to inherit members from more than one parent.

While multiple inheritance seems like a simple extension of single inheritance , 

multiple inheritance introduces a lot of issues that can markedly increase the complexity of programs and make them a maintenance nightmare.

class USBDevice
{
private:
    long m_lID;

public:
    USBDevice(long lID)
        : m_lID(lID)
    {
    }

    long GetID() { return m_lID; } };

class NetworkDevice
{
private:
    long m_lID;

public:
    NetworkDevice(long lID)
        : m_lID(lID)
    {
    }

    long GetID() { return m_lID; }
};

class WirelessAdaptor: public USBDevice, public NetworkDevice
{
public:
    WirelessAdaptor(long lUSBID, long lNetworkID)
        : USBDevice(lUSBID), NetworkDevice(lNetworkID)
    {
    }
};

int main()
{
    WirelessAdaptor c54G(5442, 181742);
    cout << c54G.GetID(); // Which GetID() do we call?

    return 0;
}
 

there is a way to work around this problem: you can explicitly specify which version you meant to call:

int main()
{
    WirelessAdaptor c54G(5442, 181742);
   cout << c54G.USBDevice::GetID();

    return 0;
}
 
Second, and more serious is the diamond problem, which your author likes to call the “diamond of doom”.
 This occurs when a class multiply inherits from two classes which each inherit from a single base class.


 

ref : http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/

arrow
arrow
    全站熱搜

    JerryCheng 發表在 痞客邦 留言(0) 人氣()