In the C++11, there is a new feature to explicitly delete functions. With this new feature, for example we can delete the copy constructor and copy-assignment operator that compiler automatically generates for us to prevent copy operation.
Before C++11
class Uncopyable {
public:
Uncopyable() {};
private:
Uncopyable(const Uncopyable& oth);
Uncopyable& operator=(const Uncopyable& oth);
};
Before C++11, this code is a solution to prevent copy operation. But it still be copyable for a friend class of Uncopyable
.
In C++11
class Uncopyable {
public:
Uncopyable() {};
Uncopyable(const Uncopyable& oth) =delete;
Uncopyable& operator=(const Uncopyable& oth) =delete;
};
In C++11, there is a more straightforward way to implement this. The =delete
marks the function to be deleted. When you do something like this
Uncopyable a, b;
b = a;
It will give you a error message at compile time.