Disable compiler-generated copy-assignment operator [duplicate]

Solution 1:

You just declare a copy constructor with private access specifier and not even define it.
Anyone trying to use it will get an compile error since it is declared private.

If someone uses it even indirectly, you will get a link error.

You can't do anything more than that in C++03.

However, In C++11 you can Explicitly delete special member functions.

Eg:

struct NonCopyable {
    NonCopyable & operator=(const NonCopyable&) = delete;
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable() = default;
};

Solution 2:

The usual way is to declare the copy constructor and the assignment operator to be private, which causes compilation errors, like Als explained.

Deriving from boost::noncopyable will do this job for you.