I give the following example to illustrate my question:
// Example program
#include <iostream>
#include <string>
class Abc
{
private:
int a_;
public:
Abc(int a):a_(a) {};
Abc& operator = ( const Abc &obj);
Abc(const Abc &obj);
};
Abc& Abc::operator = ( const Abc &obj)
{
a_ = obj.a_;
return *this;
}
Abc::Abc(const Abc &obj)
{
a_ = obj.a_;
}
int main()
{
Abc obj1(3);
Abc obj2=obj1;
return 0;
}
In the above codes, when Abc obj2=obj1
is called, I expect Abc& Abc::operator = ( const Abc &obj)
will be invoked while in fact Abc::Abc(const Abc &obj)
is called. I am confused about that. On top of it, since a_
is a private member variable, it should not be accessed while a_=obj.a_
seems to work. Any ideas? Thanks.