I have a question related to the implementation of the =
operator in C++. If I remember correctly, there are two ways of implementing =
in a class: one is to overload =
explicitly, and for example:
class ABC
{
public:
int a;
int b;
ABC& operator = (const ABC &other)
{
this->a = other.a;
this->b = other.b;
}
}
and the other is to define =
implicitly. For example:
class ABC
{
public:
int a;
int b;
ABC(const ABC &other)
{
a = other.a;
b = other.b;
}
}
My question is as follows:
1) Is it necessary to implement =
explicitly and implicitly?
2) If only one of them is necessary, which implementation is preferred?
Thanks!