This is exact definition of an user-declared copy assignment operator from current standard (§ 12.8 p. 17):
A user-declared copy assignment operator X::operator=
is a non-static
non-template member function of class X
with exactly one parameter of
type X, X&, const X&, volatile X&
or const volatile X&
.
Notes:
- An overloaded assignment operator must be declared to have only one
parameter; see 13.5.3.
- More than one form of copy assignment operator may be declared for a class.
- If a class X only has a copy assignment operator with a parameter of type X&, an expression of type const X cannot be assigned to an object of type X.
Example:
struct X {
X();
X& operator=(X&);
};
const X cx;
X x;
void f() {
x = cx; // error: X::operator=(X&) cannot assign cx into x
}
Also, please, use delete from C++11 standard.
You can now set functions as Defaulted or Deleted.
You can now write directly that you would like to disable copying.
class A {
A(const A&) = delete;
A& operator=(const A&) = delete; // Disallow copying
};
You can also explicitly inform compiler that you want default copy of a class. This way you can provide custom default constructor and still get default versions of other compiler-generated methods from compiler.
class B {
B(const Y&) = default;
B& operator=(const B&) = default; // default copy
};