我在阅读C++11 FAQ时发现了这个问题:
class X4 { ~X4() = delete; // Disallow destruction }
This implicitly also disallows moving of X4s. Copying is allowed, but deprecated.
我还找到了这句话。
Deleting the definition of a destructor will require allocation on the free-store because static and automatic objects implicitly invoke the destructor:`
struct C { ~C()= delete; //prevent automatic and static objects };
However, this technique is less useful than it may seem because it prevents delete expressions, too. Singleton objects can use it, though.`
这很有道理。我的问题是,使用一个显式删除析构函数的单例模式是否被认为是良好的实践呢?此外,如果有人知道任何其他情况下不应该调用delete
的情况,请告诉我。