17.9 Multiple inheritance
์ด์ ๊ป ๋ค๋ฃฌ inheritance๋ ๋ชจ๋ single inheritance์๋ค
ํ์ง๋ง c++์์๋ multiple inheritance๋ ์ง์ํ๊ณ ์๋ค
์ด๋ ๋ณต์์ ํด๋์ค์์ ์์๋ฐ์ ์ ์๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค
Problems with multiple inheritance
์ธ๋ป ๋ณด๊ธฐ์๋ ๊ฐ๋จํด ๋ณด์ด๋ ๋ค์ค์์์ ํ๋ก๊ทธ๋จ์ ๋ณต์กํ๊ณ ์ ์งํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ ๋ค
์ฒซ์งธ, ๋ชจํธ์ฑ์ด ๋ฐ์ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด ๋๊ฐ์ parent์ ๋๊ฐ์ ํจ์๊ฐ ์กด์ฌํ ๋ ๋ง์ด๋ค
์ด๋ฐ ๊ฒฝ์ฐ์ compiler error๊ฐ ๋ฐ์ํ๋ค
#include <iostream>
class USBDevice
{
private:
long m_id {};
public:
USBDevice(long id)
: m_id { id }
{
}
long getID() const { return m_id; }
};
class NetworkDevice
{
private:
long m_id {};
public:
NetworkDevice(long id)
: m_id { id }
{
}
long getID() const { return m_id; }
};
class WirelessAdapter: public USBDevice, public NetworkDevice
{
public:
WirelessAdapter(long usbId, long networkId)
: USBDevice { usbId }, NetworkDevice { networkId }
{
}
};
int main()
{
WirelessAdapter c54G { 5442, 181742 };
std::cout << c54G.getID(); // Which getID() do we call?
return 0;
}
์์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ parent class์ ๋ชจ๋ getID()๊ฐ ์กด์ฌํ๋ฏ๋ก compile error๋ฐ์
ํ์ง๋ง ์ด๋ฅผ ์ฐํํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. ๋ช ์์ ์ผ๋ก function call์ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค
int main()
{
WirelessAdapter c54G { 5442, 181742 };
std::cout << c54G.USBDevice::getID();
return 0;
}
์์ ๊ฐ์ด USBDevice::๋ฅผ ํตํด ์ด๋ ํ ํจ์์ธ์ง ๋ช
์์ ์ผ๋ก ์ค์ ํด์ error๋ฅผ ํผํ ์ ์๋ค
์ด๋ ๊ฒ ๋ณด๋ฉด ๊ฐ๋จํด ๋ณด์ด๋ ํด๋์ค๊ฐ ์ปค์ง๊ณ ๋ฌด๊ฑฐ์์ง ์๋ก ๋ ์ด์ ๊ฐ๋จํด์ง์ง ์๊ฒ ๋๋ค
๋์งธ, diamond problem ์ด ๋ฐ์ํ๋ค
์์ ๊ฐ์ด diamond shape์ ์์๊ด๊ณ ์ผ๋ ๋ํ๋๋ค
์ด์ ๋ํด์๋ ์ถํ ์์ธํ๊ฒ ๋ค๋ฃฌ๋ค
Is multiple inheritance more trouble than itโs worth?
๋ง์ ํ๋ก๊ทธ๋๋จธ๋ค์ ๋ค์ค์์์ ์ง์ํด์ผํ๋ค๊ณ ๋ฏฟ๋๋ค
ํ์ง๋ง ๊ทธ๋ ๋ค๊ณ ์์ ์ฐ์ง ์๋ ๊ฒ์ ์๋๋ค
iostream์ object์ธ std::cin std::cout์ multiple inheritance๋ก ๊ตฌํ๋์๋ค
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(17.9 Multiple inheritance), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@ikmy0ung/17.9-Multiple-inheritance์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค