Effective C++ item 6: Explicitly disallow the use of compiler-generated functions you do not want.

We all know from previous posts that compiler will generate constructors, destructors  and assignment operator for you. What if you don’t need or you want to prevent compiler to generate them for you? The ways is to declare the corresponding member functions private and give no implementations. Here we will have a uncopyable class to demonstrate how to implement this.

1
2
3
4
5
6
7
8
9
class Uncopyable
{
public:
Uncopyable{}
~Uncopyable{}
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
}

If you do

1
2
Uncopyable a, b;
a = b;

The compiler will complain about assignment operator is private. and if you call it in a member or a friend function, the linker will complain about can’t find implementation of this function.

Reference:
“Effective C++” Third Edition by Scott Meyers.