왜 C++에는 finally 키워드를 제공하지 않을까? #

아마 java개발자라면 의문시 되는 질문일것이다.
반대로 C++개발자로서는 왜 java나 C# 같은언어에서 저런 키워드를 제공하고 있는것일까 의문이 들수도 있을것이다.


Resource acquisition is initialization #

Stroustrup의 해답은 C++에서는 "finally" 키워드 보다 낳은 테크닉인 RAII(resource acquisition is initialization)가 있기 때문이라는 것이다.

RAII의 핵심은 소멸자에 있다
C++ 에서는 "finally"에서 해야할 동작(메모리, 리소스 해제..)을 각각의 객체의 소멸자에서 처리 할수 있기 때문에 오히려 더 OOP적인 아닌가 생각이 든다.

자기의 뒷처리(?)를 왜 남에게 맡기는가 ....


Why doesn't C++ provide a "finally" construct? #

Because C++ supports an alternative that is almost always better: The "resource acquisition is initialization" technique.

The basic idea is to represent a resource by a local object, so that the local object's destructor will release the resource. That way, the programmer cannot forget to release the resource


- "Bjarne Stroustrup's C++ Style and Technique FAQ"

+ Recent posts