C++

C++/Effective Modern C++

[Effective Modern C++] Chapter 4: Smart Pointers

raw pointer의 단점 1. 포인터의 선언에서 하나의 객체를 가리키는지 배열을 가리키는지 나타내지 않는다. 2. 포인터가 가리키는 객체에 대한 사용이 끝났을 때, 포인터가 가리키는 것을 파괴해야 하는지 여부, 즉 포인터가 가리키는 것을 포인터가 소유하고 있는지 여부에 대해 아무것도 나타내지 않는다. 3. 포인터가 가리키는 것을 파괴해야 한다고 결정했다면, 파괴할 방법을 알 방법이 없다. 4. delete가 객체를 파괴할 올바른 방법이라는 것을 알 때, delete를 사용해야 할지 delete[]를 사용해야 할지 알 수 없다. 잘못된 방법을 사용하면 정의되지 않은 결과가 발생한다. 5. 코드의 모든 경로를 따라 정확히 한 번만 파괴를 수행하도록 보장하기 어렵다. 경로를 잃으면 메모리 누수가 발생하며,..

C++/Effective Modern C++

[Effective Modern C++] Chapter 3: Moving to Modern C++(1)

Item 7: Distinguish between () and {} when casting objects C++11에서 객체 초기화는 혼란스럽다. 일반적인 규칙은, 초기화 값을 parentheses(소괄호), equal sign, 또는 braces(중괄호)로 지정하는 것이다. equal sign과 braces를 함께 사용하는 것도 가능하다. C++는 일반적으로 euqals-sign-plus-braces 문법을 braces-only와 같은 방식으로 처리한다. int x(0);// initializer is in parentheses int y = 0;// initializer follows "=" int z{0};// initializer is in braces int w = {0};// initializ..

C++/Effective Modern C++

[Effective Modern C++] Chapter 2: Auto

Item 5: Prefer auto to explicit type declarations auto를 사용하면 어떤 장점이 있는가? Auto variables must be initialized : Avoiding uninitialized variable problems auto variables는 initializer로부터 타입을 추론하기 때문에, 항상 초기화되어야 한다. 따라서, 초기화되지 않은 변수 문제로부터 벗어날 수 있다. Represent types knwon only to compilers auto는 type deduction을 사용하기 때문에, 컴파일러에만 있는 타입을 지정할 수 있다. Auto vs std::function std::function은 C++11 표준 라이브러리의 템플릿으로,..

C++/Effective Modern C++

[Effective Modern C++] Chapter 1: Deducing Types

Item 1 : Understand template type deduction Type deduction for templates는 modern C++의 auto를 기반으로 한다. template void f(ParamType param); f(expr); 위 코드는, 컴파일 시간 동안 T와 ParamType이라는 두가지 타입을 추론하기 위해 expr을 사용한다. 보통 ParamType은 종종 const, reference와 같은 adornment를 포함하기 때문에 두 타입은 자주 다르다. 예를들어, template이 다음과 같이 선언된 경우, template void f(const T& param); int x = 0; f(x); T는 int로 추론되지만, ParamType은 const int&로 추론..

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가 설정되고, 호..

Tuesberry
'C++' 카테고리의 글 목록