SlideShare a Scribd company logo
1 of 17
Download to read offline
Effective C++
item40. 다중 상속은 심사숙고해서 사용하자
이데아게임즈 손진화
error C2385 : 'checkOut' 액세스가 모호합니다.
'BorrowableItem'의 'checkOut'일 수 있습니다.
'ElectronicGadget'의 'checkOut'일 수 있습니다.
다중 상속으로 인한 모호성
눈으로 봐도 애매하다
error C2385 : 'checkOut' 액세스가 모호합니다.
'BorrowableItem'의 'checkOut'일 수 있습니다.
'ElectronicGadget'의 'checkOut'일 수 있습니다.
다중 상속으로 인한 모호성
눈으로 봤을 때는
ElectronicGadget 의
함수에 접근 권한이
없기 때문에 잘 동작
할 것 처럼 보인다
다중 상속으로 인한 모호성
C++ 컴파일러는 호출에 대해 최적으로 일치하는 함수를 먼저 찾은
다음에 그 함수에 접근 할 수 있는 지를 체크한다 그래서 앞의
상황에서 컴파일러는 오류를 출력한다
호출할 함수를 명시적으로 지정해준다면 모호성을 해결할 수 있다
죽음의 MI 마름모꼴
Deadly MI(multiple inheritance) diamond
File
InputFile OutputFile
IOFile
죽음의 MI 마름모꼴
error C2385: 'm_FileName' 액세스가 모호합니다.
기본 'File'의 'm_FileName'일 수 있습니다.
또는 기본 'File'의 'm_FileName'일 수 있습니다.
기본 클래스 File 이 중복되어
생성되었음을 알 수 있다
가상 상속
중복되는 클래스를
가상 기본 클래스로 만들면
문제를 해결 할 수 있다
단, 가상 상속을 받은 클래스는
데이터가 더 크며
기본 데이터 멤버에 접근하는
속도가 느리다
표준 C++ 라이브리에 있는 가상 상속
This class inherits all members from its two basic_istream and
basic_ostream (using virtual inheritance), thus being able to
perform both input and output operations.
basic_ios
basic_istream Basic_ostream
basic_iostream
가상 기본 클래스 초기화 문제
가상 기본 클래스 초기화 문제
가상 기본 클래스 초기화 문제
가상 기본 클래스 초기화 문제
1. 초기화가 필요한 가상 기본 클래스로부터 클래스가 파생된 경우,
이 파생 클래스는 가상 기본 클래스와의 거리에 상관없이 가상 기본
클래스의 존재를 염두에 두고 있어야 한다
2. 기존의 클래스 계통에 파생 클래스를 새로 추가할 때도 그 파생
클래스는 가상 기본 클래스(역시 거리에 상관 없이)의 초기화를
떠맡아야 한다
결론
1. 구태여 쓸 필요가 없으면 가상 기본 클래스를 사용하지 마라
2. 가상 기본 클래스를 정말 써야 하는 상황이라면 가상 기본
클래스에는 데이터를 넣지 않는 쪽으로 하라
권장하는 다중 상속 방법
한 클래스로부터 인터페이스 상속을 받고 다른 클래스로부터 구현
상속을 받는다
인터페이스를 가져오고자 하는 클래스
권장하는 다중 상속 방법
구현을 가져오고자 하는 클래스
권장하는 다중 상속 방법
원하는 결과
권장하는 다중 상속 방법
구현
이것만은 잊지 말자!
• 다중 상속은 단일 상속보다 확실히 복잡합니다 새로운 모호성
문제를 일으킬 뿐만 아니라 가상 상속이 필요해질 수도 있습니다
• 가상 상속을 쓰면 크기 비용, 속도 비용이 늘어나며, 초기화 및 대입
연산의 복잡도가 커집니다 따라서 가상 기본 클래스에는 데이터를
두지 않는 것이 현실적으로 가장 실용적입니다
• 다중 상속을 적법하게 쓸 수 있는 경우가 있습니다 여러 시나리오 중
하나는 인터페이스 클래스로부터 public 상속을 시킴과 동시에
구현을 돕는 클래스로부터 private 상속을 시키는 것입니다

More Related Content

Similar to Effective c++ item40

Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Ryan Park
 
카사 공개세미나1회 W.E.L.C.
카사 공개세미나1회  W.E.L.C.카사 공개세미나1회  W.E.L.C.
카사 공개세미나1회 W.E.L.C.Ryan Park
 
More Effective C++ 4주차
More Effective C++ 4주차More Effective C++ 4주차
More Effective C++ 4주차Injae Lee
 
HolubOnPatterns/chapter2_2
HolubOnPatterns/chapter2_2HolubOnPatterns/chapter2_2
HolubOnPatterns/chapter2_2SeungHyun Hwang
 
Effective java
Effective javaEffective java
Effective javaHaeil Yi
 
Effective c++ 1,2
Effective c++ 1,2Effective c++ 1,2
Effective c++ 1,2세빈 정
 
Tcpl 12장 파생클래스
Tcpl 12장 파생클래스Tcpl 12장 파생클래스
Tcpl 12장 파생클래스재정 이
 
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지GDG Korea
 
Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Daum DNA
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinDong Chan Shin
 
Legacy code refactoring video rental system
Legacy code refactoring   video rental systemLegacy code refactoring   video rental system
Legacy code refactoring video rental systemJaehoon Oh
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinDong Chan Shin
 
About Visual C++ 10
About  Visual C++ 10About  Visual C++ 10
About Visual C++ 10흥배 최
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 pptInjae Lee
 
[C언어]함수오버로딩과오버라이딩
[C언어]함수오버로딩과오버라이딩[C언어]함수오버로딩과오버라이딩
[C언어]함수오버로딩과오버라이딩jusingame
 
Chapter5 ~ 6
Chapter5 ~ 6Chapter5 ~ 6
Chapter5 ~ 6Injae Lee
 
Effective c++ Chapter1,2
Effective c++ Chapter1,2Effective c++ Chapter1,2
Effective c++ Chapter1,2문익 장
 
Effective c++ 1~8장
Effective c++ 1~8장 Effective c++ 1~8장
Effective c++ 1~8장 Shin heemin
 
Effective c++ 3
Effective c++ 3Effective c++ 3
Effective c++ 3현찬 양
 
The roadtocodecraft
The roadtocodecraftThe roadtocodecraft
The roadtocodecraftbbongcsu
 

Similar to Effective c++ item40 (20)

Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005
 
카사 공개세미나1회 W.E.L.C.
카사 공개세미나1회  W.E.L.C.카사 공개세미나1회  W.E.L.C.
카사 공개세미나1회 W.E.L.C.
 
More Effective C++ 4주차
More Effective C++ 4주차More Effective C++ 4주차
More Effective C++ 4주차
 
HolubOnPatterns/chapter2_2
HolubOnPatterns/chapter2_2HolubOnPatterns/chapter2_2
HolubOnPatterns/chapter2_2
 
Effective java
Effective javaEffective java
Effective java
 
Effective c++ 1,2
Effective c++ 1,2Effective c++ 1,2
Effective c++ 1,2
 
Tcpl 12장 파생클래스
Tcpl 12장 파생클래스Tcpl 12장 파생클래스
Tcpl 12장 파생클래스
 
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지
GKAC 2015 Apr. - 테스트 코드에서 코드 커버리지까지
 
Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기Devon 2011-b-5 효과적인 레거시 코드 다루기
Devon 2011-b-5 효과적인 레거시 코드 다루기
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshin
 
Legacy code refactoring video rental system
Legacy code refactoring   video rental systemLegacy code refactoring   video rental system
Legacy code refactoring video rental system
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshin
 
About Visual C++ 10
About  Visual C++ 10About  Visual C++ 10
About Visual C++ 10
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 ppt
 
[C언어]함수오버로딩과오버라이딩
[C언어]함수오버로딩과오버라이딩[C언어]함수오버로딩과오버라이딩
[C언어]함수오버로딩과오버라이딩
 
Chapter5 ~ 6
Chapter5 ~ 6Chapter5 ~ 6
Chapter5 ~ 6
 
Effective c++ Chapter1,2
Effective c++ Chapter1,2Effective c++ Chapter1,2
Effective c++ Chapter1,2
 
Effective c++ 1~8장
Effective c++ 1~8장 Effective c++ 1~8장
Effective c++ 1~8장
 
Effective c++ 3
Effective c++ 3Effective c++ 3
Effective c++ 3
 
The roadtocodecraft
The roadtocodecraftThe roadtocodecraft
The roadtocodecraft
 

More from 진화 손

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf진화 손
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf진화 손
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates진화 손
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...진화 손
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions진화 손
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf진화 손
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types진화 손
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements진화 손
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library진화 손
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine진화 손
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules진화 손
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules진화 손
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)진화 손
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains진화 손
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers진화 손
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]진화 손
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts진화 손
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>진화 손
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr진화 손
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields진화 손
 

More from 진화 손 (20)

C++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdfC++20 Remove std::weak_equality and std::strong_equality.pdf
C++20 Remove std::weak_equality and std::strong_equality.pdf
 
C++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdfC++20 std::execution::unseq.pdf
C++20 std::execution::unseq.pdf
 
C++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templatesC++ 20 class template argument deduction for alias templates
C++ 20 class template argument deduction for alias templates
 
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
C++ 20 Make stateful allocator propagation more consistent for operator+(basi...
 
C++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functionsC++ 20 Unevaluated asm-declaration in constexpr functions
C++ 20 Unevaluated asm-declaration in constexpr functions
 
C++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdfC++20 Utility functions to implement uses-allocator construction.pdf
C++20 Utility functions to implement uses-allocator construction.pdf
 
C++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete typesC++ 20 std__reference_wrapper for incomplete types
C++ 20 std__reference_wrapper for incomplete types
 
C++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirementsC++ 20 Stronger Unicode requirements
C++ 20 Stronger Unicode requirements
 
C++20 Concepts library
C++20 Concepts libraryC++20 Concepts library
C++20 Concepts library
 
C++20 Coroutine
C++20 CoroutineC++20 Coroutine
C++20 Coroutine
 
C++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rulesC++ 20 Relaxing the range-for loop customization point finding rules
C++ 20 Relaxing the range-for loop customization point finding rules
 
C++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rulesC++ 20 Relaxing the structured bindings customization point finding rules
C++ 20 Relaxing the structured bindings customization point finding rules
 
C++20 explicit(bool)
C++20 explicit(bool)C++20 explicit(bool)
C++20 explicit(bool)
 
C++20 std::map::contains
C++20 std::map::containsC++20 std::map::contains
C++20 std::map::contains
 
C++20 Comparing unordered containers
C++20 Comparing unordered containersC++20 Comparing unordered containers
C++20 Comparing unordered containers
 
C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]C++20 Attributes [[likely]] and [[unlikely]]
C++20 Attributes [[likely]] and [[unlikely]]
 
C++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contextsC++ 20 Lambdas in unevaluated contexts
C++ 20 Lambdas in unevaluated contexts
 
C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>C++20 Library support for operator<=> <compare>
C++20 Library support for operator<=> <compare>
 
C++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptrC++20 Atomic std::shared_ptr and std::weak_ptr
C++20 Atomic std::shared_ptr and std::weak_ptr
 
C++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fieldsC++20 Default member initializers for bit-fields
C++20 Default member initializers for bit-fields
 

Effective c++ item40

  • 1. Effective C++ item40. 다중 상속은 심사숙고해서 사용하자 이데아게임즈 손진화
  • 2. error C2385 : 'checkOut' 액세스가 모호합니다. 'BorrowableItem'의 'checkOut'일 수 있습니다. 'ElectronicGadget'의 'checkOut'일 수 있습니다. 다중 상속으로 인한 모호성 눈으로 봐도 애매하다
  • 3. error C2385 : 'checkOut' 액세스가 모호합니다. 'BorrowableItem'의 'checkOut'일 수 있습니다. 'ElectronicGadget'의 'checkOut'일 수 있습니다. 다중 상속으로 인한 모호성 눈으로 봤을 때는 ElectronicGadget 의 함수에 접근 권한이 없기 때문에 잘 동작 할 것 처럼 보인다
  • 4. 다중 상속으로 인한 모호성 C++ 컴파일러는 호출에 대해 최적으로 일치하는 함수를 먼저 찾은 다음에 그 함수에 접근 할 수 있는 지를 체크한다 그래서 앞의 상황에서 컴파일러는 오류를 출력한다 호출할 함수를 명시적으로 지정해준다면 모호성을 해결할 수 있다
  • 5. 죽음의 MI 마름모꼴 Deadly MI(multiple inheritance) diamond File InputFile OutputFile IOFile
  • 6. 죽음의 MI 마름모꼴 error C2385: 'm_FileName' 액세스가 모호합니다. 기본 'File'의 'm_FileName'일 수 있습니다. 또는 기본 'File'의 'm_FileName'일 수 있습니다. 기본 클래스 File 이 중복되어 생성되었음을 알 수 있다
  • 7. 가상 상속 중복되는 클래스를 가상 기본 클래스로 만들면 문제를 해결 할 수 있다 단, 가상 상속을 받은 클래스는 데이터가 더 크며 기본 데이터 멤버에 접근하는 속도가 느리다
  • 8. 표준 C++ 라이브리에 있는 가상 상속 This class inherits all members from its two basic_istream and basic_ostream (using virtual inheritance), thus being able to perform both input and output operations. basic_ios basic_istream Basic_ostream basic_iostream
  • 9. 가상 기본 클래스 초기화 문제
  • 10. 가상 기본 클래스 초기화 문제
  • 11. 가상 기본 클래스 초기화 문제
  • 12. 가상 기본 클래스 초기화 문제 1. 초기화가 필요한 가상 기본 클래스로부터 클래스가 파생된 경우, 이 파생 클래스는 가상 기본 클래스와의 거리에 상관없이 가상 기본 클래스의 존재를 염두에 두고 있어야 한다 2. 기존의 클래스 계통에 파생 클래스를 새로 추가할 때도 그 파생 클래스는 가상 기본 클래스(역시 거리에 상관 없이)의 초기화를 떠맡아야 한다 결론 1. 구태여 쓸 필요가 없으면 가상 기본 클래스를 사용하지 마라 2. 가상 기본 클래스를 정말 써야 하는 상황이라면 가상 기본 클래스에는 데이터를 넣지 않는 쪽으로 하라
  • 13. 권장하는 다중 상속 방법 한 클래스로부터 인터페이스 상속을 받고 다른 클래스로부터 구현 상속을 받는다 인터페이스를 가져오고자 하는 클래스
  • 14. 권장하는 다중 상속 방법 구현을 가져오고자 하는 클래스
  • 15. 권장하는 다중 상속 방법 원하는 결과
  • 16. 권장하는 다중 상속 방법 구현
  • 17. 이것만은 잊지 말자! • 다중 상속은 단일 상속보다 확실히 복잡합니다 새로운 모호성 문제를 일으킬 뿐만 아니라 가상 상속이 필요해질 수도 있습니다 • 가상 상속을 쓰면 크기 비용, 속도 비용이 늘어나며, 초기화 및 대입 연산의 복잡도가 커집니다 따라서 가상 기본 클래스에는 데이터를 두지 않는 것이 현실적으로 가장 실용적입니다 • 다중 상속을 적법하게 쓸 수 있는 경우가 있습니다 여러 시나리오 중 하나는 인터페이스 클래스로부터 public 상속을 시킴과 동시에 구현을 돕는 클래스로부터 private 상속을 시키는 것입니다