effective C++

C++/Effective C++

[Effective C++] Chapter 8: Customizing new and delete

Item 49 : Understand the behavior of the new-handler new 처리자의 동작원리를 제대로 이해하자 operator new는 메모리 할당 요청을 만족하지 못하면, 예외를 발생시킨다. 오랫동안, 그것은 null pointer를 리턴했다. operator new를 throw하기 전에, new-handler라는 클라이언트가 지정한 error-handling function을 호출할 수 있다. out-of-memory-handling function을 지정하기 위해, 클라이언트는 std의 set_new_handler를 호출한다. namespace std{ typedef void (*new_handler)(); new_handler set_new_handler(new_hand..

C++/Effective C++

[Effective C++] Chapter 7: Templates and Generic Programming(2)

Item 45 : Use member function templates to accept "all compatible types" 호환되는 모든 타입을 받아들이는 데는 멤버 함수 템플릿이 직방! 스마트포인터는 포인터처럼 동작하는 객체지만, 포인터가 제공하지 않는 추가적인 기능을 제공한다. real pointer가 잘하는 것 중 하나는 implicit conversion을 지원하는 것이다. Derived class pointer는 암시적으로 base class pointer로 변환될 수 있고, non-const 객체에 대한 포인터는 const 객체에 대한 포인터로 암시적 변환될 수 있다. user-defined smart pointer class에 대해 고려해보자. template class SmartPt..

C++/Effective C++

[Effective C++] Chapter 7: Templates and Generic Programming(1)

Item 41 : Understand implicit interfaces and compile-time polymorphism 템플릿 프로그래밍의 천릿길도 암시적 인터페이스와 컴파일 타입 다형성부터. class Widget{ public: Widget(); virtual ~Widget(); virtual std::size_t size() const; virtual void normalize(); void swap(Widget& other); }; void doProcessing(Widget& w) { if(w.size() > 10 && w != someNastyWidget){ Widget temp(w); temp.normalize(); temp.swap(w); } } 클래스의 경우, 코드를 통해 inte..

C++/Effective C++

[Effective C++] Chapter 6: Inheritance and Object-Oriented Design(2)

Item 35 : Consider alternatives to virtual functions 가상 함수 대신 쓸 것들도 생각해 두자. 1. The Template Method Pattern via the Non-Virtual Interface(NVI) Idiom 이 전략을 사용하는 경우 virtual function은 private으로 둬야 한다. non-virtual 멤버 함수를 만들고 non-virtual 멤버 함수가 private virtual function을 호출하도록 한다. 사전 동작과 사후 동작이 가능하다는 장점이 있다. virtual function이 호출되기 이전, 호출된 후 특정한 작업을 수행할 수 있도록 보장한다. 즉, 가상 함수가 호출되기 전에 적절한 context가 설정되고, 호..

C++/Effective C++

[Effective C++] Chapter 6: Inheritance and Object-Oriented Design(1)

Item 32 : Make sure public inheritance models "is-a" Object-Oriented programming의 가장 중요한 규칙은 public inheritance가 "is-a"를 의미한다는 것이다. 만약 Base class를 public으로 상속하는 Derived class가 있다고 가정하자. 모든 Derived class type의 객체는 Base class type의 객체가 되지만, 반대는 틀리다. Base class는 Derived class보다 더 일반적인 개념을, Derived class는 Base class보다 더 specialized된 개념을 ㅍ현한다. 객체의 타입이 Base class인 객체가 사용되는 어떤 곳이든 Derived class 타입의 객체를..

C++/Effective C++

[Effective C++] Chapter 5: Implementations(2)

Item 29 : Strive for exception-safe code 아래의 코드를 살펴보자. void PrettyMenu::ChangeBackground(std::istream& imgSrc) { lock(&mutex); delete dbImage; ++ImageChanges; bgImage = new Image(imgSrc); unlock(&mutex); } 예외 안전성의 측면에서, 이 함수는 나쁘다. 예외가 발생한 경우, exception safe 함수는 다음과 같은 사항을 만족해야 한다. 1. Leak no resources 위 함수는 new Image(imgSrc)에서 예외가 발생하면, unclock함수가 절대 호출되지 않는다. 따라서 nutex를 영원히 가지게 된다. 2. Don't all..

C++/Effective C++

[Effective C++] Chapter 5: Implementations(1)

Item 26 : Postpone variable definitions as long as possible 변수의 정의를 변수를 바로 사용해야할 때 직전에 해야할 뿐만 아니라, 변수를 위한 초기화 값을 가지기 전까지 변수 정의를 연기해야 한다. 이렇게 함으로써, 불필요한 객체에 대한 생성자와 소멸자가 호출되는 것을 피하고, 의미가 명확한 상황에서 변수를 초기화하여 변수의 목적을 문서화하는데 도움이 된다. loop를 사용하는 경우, 변수를 loop문 안에 정의하거나, 밖에 정의할 수 있다. 할당이 생성자-소멸자보다 비용이 적고, 코드의 성능에 민감한 부분을 처리하지 않는 한 기본적으로 변수를 안에 정의해야한다. Item 27 : Minimize casting 캐스팅은 type system을 파괴하며 모든 ..

C++/Effective C++

[Effective C++] Chapter 4: Design and Declarations(2)

Item 22 : Declare data members private 왜 data member가 public이어야 하는가? 1. syntax consistency 만약 멤버 데이터가 public이 아니라면, 클라이언트에 객체에 접근할 수 있는 유일한 방법은 멤버 함수 뿐이다. 모든 public interface가 function이면, 모든 것이 함수이기 때문에 클라이언트는 자연스럽게 함수를 사용할 수 있다. 2. access control 멤버 데이터가 public이 아니라면, 멤버 데이터에 대한 접근 권한이 없는 경우, 읽기 전용, 읽고 쓰기 가능 등 다양한 접근 권한에 대한 구현이 가능하다. 그러나 멤버 데이터가 public이면, 모든 멤버 데이터를 읽고 쓸 수 있다. 모든 데이터 멤버가 hidden..

Tuesberry
'effective C++' 태그의 글 목록