c++ - 为什么在 C++ 中使用私有(private)复制构造函数与删除的复制构造函数

标签 c++

为什么人们宁愿使用私有(private)复制构造函数而不是删除 C++ 中的复制构造函数?

例如:

class Entity
{
private:
    Entity(const Entity &copy) // <== private copy constructor
    {
       /* do copy stuff */
    }
public:
    /* more code here... */
}

相对于:

class Entity
{
public:
    Entity(const Entity &copy) = delete; // <== copy constructor deleted

    /* more code here... */
}

无法完全回答我的问题的相关问题:

What's the use of the private copy constructor in c++

Deleted vs empty copy constructor

最佳答案

2个可能的原因:

  • 您不能使用 C++11 或更高版本

  • 您需要类的对象可以被类或其友元的方法复制,但不能被其他任何东西复制

关于c++ - 为什么在 C++ 中使用私有(private)复制构造函数与删除的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53127611/

相关文章:

c++ - 从 LabVIEW 调用的 DLL 写入 MATLAB 文件

c++ - 有使用英特尔线程构建模块的经验吗?

java - 使用 android NativeActivity 时从 SoftInput 接收所有 unicode 字符

c++ - 表达式必须是可修改的左值

c++ - 为什么 lambda 只捕获自动存储变量?

c++ - 我从哪里可以获得适用于 Mac 的 windows.h?

c++ - Winsock 替代品(不,不是 WinPCAP)

c++ - Windows 和 Linux 中使用的可执行二进制格式的低级解释

c++ - 为什么包含 std::stringstream 的类的 move 语义会导致编译器错误?

c++ - 获取正确的 value_type