Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern name for create in constructor, delete in destructor (C++)

Traditionally, in C++, you would create any dependencies in the constructor and delete them in the destructor.

class A
{
 public:
    A() { m_b = new B(); }
    ~A() { delete m_b; }
 private:
    B* m_b;
};

This technique/pattern of resource acquisition, does it have a common name?
I'm quite sure I've read it somewhere but can't find it now.

Edit:
As many has pointed out, this class is incomplete and should really implement a copy constructor and assignment operator.
Originally, I intentionally left it out since it wasn't relevant to the actual question: the name of the pattern. However, for completeness and to encourage good practices, the accepted answer is what it is.

like image 997
Zen Avatar asked Dec 04 '09 10:12

Zen


1 Answers

RAII - Resource Acquisition Is Initialization

like image 53
Jonathan Leffler Avatar answered Oct 28 '22 23:10

Jonathan Leffler