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

后端 未结 3 1998
清歌不尽
清歌不尽 2020-12-16 17:16

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(); }
         


        
3条回答
  •  渐次进展
    2020-12-16 18:12

    This technique is best known as RAII - Resource Allocation Is Initialization. It has its own tag on this site.

    Alternative. more intuitive names have been suggested, in particular:

    • UDSTMR - Using Destructor Semantics To Manage Resources.
    • UTSTTC - Using The Stack To Trigger Cleanup.
    • LECLEOEIGU - Lifetime Equals Class Lifetime Or Else It Gets Ugly (the original suggestion, LECCLEOEIGU - Lifetime Equals C++ Class Lifetime Or Else It Gets Ugly, includes the language name, which I prefer to remove in this answer because the language is known).

提交回复
热议问题