SlideShare a Scribd company logo
1 of 11
Download to read offline
Effective C++
item9. 객체 생성 및 소멸 과정 중에는
절대로 가상 함수를 호출하지 말자
이데아게임즈 손진화
기본 생성자 호출 순서
기본 생성자 호출 순서
- 기본 클래스 생성자가 호출되는 시점에서는
파생 클래스가 초기화 되지 않은 상태이다
- 기본 클래스 생성자에서 파생 클래스의 멤버에 접근하는 것은
미정의 동작을 유발할 수 있기 때문에 기본 클래스 타입으로
동작한다
기본 생성자 호출 순서
- 기본 클래스 생성자가 호출되는 시점에서는
파생 클래스가 초기화 되지 않은 상태이다
- 기본 클래스 생성자에서 파생 클래스의 멤버에 접근하는 것은
미정의 동작을 유발할 수 있기 때문에 기본 클래스 타입으로
동작한다
정의 된 가상 함수 호출
정의 된 가상함수 호출
- 파생 클래스의 생성자 실행이 시작되어야만
그 객체가 파생 클래스로서 동작하게 된다
- 기본 클래스 생성자에서 가상 함수 호출 시
의도하지 않은 동작이 일어날 수 있다
순수 가상 함수 호출
주의
소멸자도 마찬가지
기본 클래스 생성자에서 파생 클래스에 따라
다른 처리를 해줘야 한다면 이렇게 하자
기본 클래스 생성자에서 파생 클래스에 따라
다른 처리를 해줘야 한다면 이렇게 하자
- 생성자가 비가상 함수를 호출하게 하는 대신 필요한 정보를
생성자로 전달한다
- 전달할 데이터는 정적 멤버로 선언하라

More Related Content

More from 진화 손

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진화 손
 
C++ 20 constexpr for algorithm and utility
C++ 20 constexpr for  algorithm and utilityC++ 20 constexpr for  algorithm and utility
C++ 20 constexpr for algorithm and utility진화 손
 

More from 진화 손 (20)

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
 
C++ 20 constexpr for algorithm and utility
C++ 20 constexpr for  algorithm and utilityC++ 20 constexpr for  algorithm and utility
C++ 20 constexpr for algorithm and utility
 

Effective c++ item9

  • 1. Effective C++ item9. 객체 생성 및 소멸 과정 중에는 절대로 가상 함수를 호출하지 말자 이데아게임즈 손진화
  • 3. 기본 생성자 호출 순서 - 기본 클래스 생성자가 호출되는 시점에서는 파생 클래스가 초기화 되지 않은 상태이다 - 기본 클래스 생성자에서 파생 클래스의 멤버에 접근하는 것은 미정의 동작을 유발할 수 있기 때문에 기본 클래스 타입으로 동작한다
  • 4. 기본 생성자 호출 순서 - 기본 클래스 생성자가 호출되는 시점에서는 파생 클래스가 초기화 되지 않은 상태이다 - 기본 클래스 생성자에서 파생 클래스의 멤버에 접근하는 것은 미정의 동작을 유발할 수 있기 때문에 기본 클래스 타입으로 동작한다
  • 5. 정의 된 가상 함수 호출
  • 6. 정의 된 가상함수 호출 - 파생 클래스의 생성자 실행이 시작되어야만 그 객체가 파생 클래스로서 동작하게 된다 - 기본 클래스 생성자에서 가상 함수 호출 시 의도하지 않은 동작이 일어날 수 있다
  • 10. 기본 클래스 생성자에서 파생 클래스에 따라 다른 처리를 해줘야 한다면 이렇게 하자
  • 11. 기본 클래스 생성자에서 파생 클래스에 따라 다른 처리를 해줘야 한다면 이렇게 하자 - 생성자가 비가상 함수를 호출하게 하는 대신 필요한 정보를 생성자로 전달한다 - 전달할 데이터는 정적 멤버로 선언하라