camercu
camercu

Reputation: 147

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

E.g.:

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

Upvotes: 2

Views: 817

Answers (1)

Slava
Slava

Reputation: 44238

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

Upvotes: 3

Related Questions