What is meant by Resource Acquisition is Initialization (RAII)?

前端 未结 8 1687
面向向阳花
面向向阳花 2020-11-21 04:23

What is meant by Resource Acquisition is Initialization (RAII)?

8条回答
  •  臣服心动
    2020-11-21 04:48

    There are three parts to an RAII class:

    1. The resource is relinquished in the destructor
    2. Instances of the class are stack allocated
    3. The resource is acquired in the constructor. This part is optional, but common.

    RAII stands for "Resource Acquisition is initialization." The "resource acquisition" part of RAII is where you begin something that must be ended later, such as:

    1. Opening a file
    2. Allocating some memory
    3. Acquiring a lock

    The "is initialization" part means that the acquisition happens inside the constructor of a class.

    https://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/

提交回复
热议问题