How does =delete on destructor prevent stack allocation?

后端 未结 6 1819
南旧
南旧 2020-12-05 05:49

In this SO question is stated that this construct prevents stack allocation of instance.

class FS_Only {
    ~FS_Only() = delete;  // disallow stack allocati         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 06:31

    The only way to allocate such an object would be through dynamic allocation, without any de-alocation (the object will either be leaked, or accessed by pointer for the duration of the program.

    Any attempt to allocate a static instance will cause a compilation error when the allocated object would go out of scope (as the destructor code cannot be called).

提交回复
热议问题