What exactly happens at 1 & 2 internally , when we have assigned by return reference?
Class A
{
A& operator= (const A &ax)
{
return *this;
}
}
int main()
{
A a;
A b;
b = a; // -------------- 1
A c = a; // ----------- 2
}
// ---------------------------------------------------------------
What difference will happen in below (assigned returned value)? :
Class A
{
A operator= (const A &ax)
{
return *this;
}
}
int main()
{
A a;
A b;
b = a; // -------------- 3
A c = a; // ----------- 4
}
There is also principal mistake, because
operator=made private and code is ill-formed, but this would be corrected by addingpublic:access modifiers.operator=is invoked from object stored tobvariable, so this variant of assignment should not perform assignment at all.The line
is called initialization and doesn't use
operator=, instead it uses (generated by compiler) copy constructor, which does a shallow copy of object.Your mistake is that member operator= meant to assign its argument's value to
this.operator=return result of assignment, which can be used for chaining:a=b=c;is equivalent of