then the moral of the story would be to always overload the = operator if you overload the copy constructor.
Yes. I think so too. Both the implicitly declared operator=() and copy constructor does a member-wise copy of each data elements in the class. If we do not like this default behaviour ( like for classes with dynamically maintained members) we have to overload them. If we don't like a member-wise copy behaviour for one method, I don't think we will like the default for the other. So effectively both should be overloaded if used.
I think the reason Wolfpacks code works with the default assignment operator provided by the compiler whereas vicky_devs does not when this:
s3 = s1 + s2;
is tried has more to do with the default version of the assignment operator rather than the code of the overloaded + operator.
Most probably. I created my version by changing vicky's program a little.
Anyway I think the original problem was about
why does
STRING s3 = s1 + s2;
call the copy constructor and
why does NOT
STRING s3;
s3 = s1 + s2 ;
call the copy constructor. I think the reason should be clear by now.
STRING s3 = s1 + s2;
is equivalent to
STRING s3(s1+s2);
where as
STRING s3;
s3 = s1 + s2 ;
is a default construction and assignment. No copy constructors involved here.
I think this link makes the difference between these initializations much clearer. More specifically look at the part
SomeType t = u;
This is copy initialisation, and the variable t is always initialised using SomeType's copy ctor. (Even though there's an "=" there, that's just a syntax holdover from C... this is always initialisation, never assignment, and so operator= is never called.)