简体   繁体   English

为什么在C ++中使用私有副本构造函数与已删除副本构造函数

[英]Why use private copy constructor vs. deleted copy constructor in C++

Why would one prefer to use a private copy constructor over deleting the copy constructor in C++? 为什么要在C ++中删除复制构造函数而不是使用私有复制构造函数?

Eg: 例如:

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

As opposed to: 相对于:

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

    /* more code here... */
}

Related questions that don't quite answer what I'm asking: 未能完全回答我所问问题的相关问题:

What's the use of the private copy constructor in c++ C ++中的私有副本构造函数有什么用

Deleted vs empty copy constructor 删除vs空副本构造函数

2 possible reasons: 2个可能的原因:

  • you cannot use C++11 or later 您不能使用C ++ 11或更高版本

  • you need objects of the class to be copyable by methods of the class or it's friends, but not by anything else 您需要该类的对象可以通过该类的方法或其朋友来复制,但不能通过其他任何方法复制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM