7월 12, 2016

[EC++ 05] Know what functions C++ silently writes and calls.

  1. 사용자가 직접 선언하지 않아도 컴파일러가 자동적으로 정의하는 function이 존재.
  2. copy constructor, copy assignment operator, and destructor 등. 만약 사용자가 constructor를 하나도 직접 선언하지 않았다면 컴파일러가 default constructor 도 자동으로 선언. 이러한 function들은 모두 public, inline 함수로 선언됨.

  3. 상속 관계가 있는 클래스의 destructor 선언 시 주의
  4. Base class의 destructor가 virtual로 선언되지 않은 경우, derived class의 compiler-generated destructor는 non-virtual로 선언됨.

  5. Default construtor가 자동으로 생성되지 않는 경우
  6. 사용자가 단 하나의 constructor라도 직접 선언한다면 default constructor는 자동으로 생성되지 않음.

  7. 그 외 함수가 자동으로 생성되는 경우
  8. Copy constructor나 copy assignment operator의 사용자 선언이 없으면, 이러한 함수가 실제로 call 되며, legal하게 작동되는 경우에 한해 컴파일러가 함수를 생성할 수 있음.

  9. 컴파일러는 derived class가 base class에서 private으로 선언된 copy assignment operator를 implicitly call하는 것을 허용하지 않음.

Summary

  • Compilers may implicitly generate a class's default constructor, copy constructor, copy assignment operator, and destructor.

Reference

  • Effective C++ by Scott Meyers

댓글 없음:

댓글 쓰기