简体   繁体   中英

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++?

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++

Deleted vs empty copy constructor

2 possible reasons:

  • you cannot use C++11 or later

  • you need objects of the class to be copyable by methods of the class or it's friends, but not by anything else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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