C++11 析构函数 = 删除

标签 c++ c++11 destructor

我看过一个constructor = delete的解释here但我想知道我是否也应该禁止析构函数调用。我正在尝试使用这样的类:

class A
{
public:
    static bool foo(const char* filePath);
    static void foo(const int something);
private:
    A() = delete;
    ~A();
};

我是否也应该像 ~A() = delete; 这样写?这有关系吗?

最佳答案

~A() = delete; 是多余的,因为您不能创建对象,所以不必担心析构函数。

事实上,对于您的代码,甚至不需要A() = delete;,因为所有类成员都是static
正如 Luchian 在评论中正确提到的那样,这样的 class 最好声明为 namespace。底层数据可以根据需要做成extern/static

关于C++11 析构函数 = 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210382/

相关文章:

c++ - 为什么用 A{8} 初始化的对象不能作为参数传递?

c++ - 启动 std::thread 时无法将(函数指针)左值绑定(bind)到(函数指针)右值?

c++ - `*this` 可以是 `move()` d 吗?

c++ - 映射析构函数错误

php - 在析构函数中保存 - 坏主意?

c# - 将 C++ 代码导出为 DLL 并使用 C# 导入?

c++ - Corba 请求超时

c++ - 部分破坏的物体?

c++ - 另一个小内存 C++ 问题

c++ - 为什么在给定 T&& 和 const T& 重载的情况下,const 左值与 const 右值的绑定(bind)不同?