0

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.)

0

Ok, I think I am beginning to understand, finally...let me get this straight...

When I write string s3; , the default constrcutor is called and so s3 is empty.

Then when s3 = s1 + s2; only assignment occurs, no copy constrcutor involved, that will be bit-by-bit I think. So no memory allocation, s3 will still be empty.

On the other hand, in case I write string s3 = s1 + s2 , copy constructor is called, so no problems there.:mrgreen:

Is that it?:?:

One more thing...when an object is passed by value, then copy constructor is called, and also while returning an object, I hope...:idea:

0

Ok, I think I am beginning to understand, finally...let me get this straight...

When I write string s3; , the default constrcutor is called and so s3 is empty.

Then when s3 = s1 + s2; only assignment occurs, no copy constrcutor involved, that will be bit-by-bit I think. So no memory allocation, s3 will still be empty.

On the other hand, in case I write string s3 = s1 + s2 , copy constructor is called, so no problems there.:mrgreen:

Is that it?:?:

Yes.

One more thing...when an object is passed by value, then copy constructor is called, and also while returning an object, I hope...:idea:

Yes. That is why it is discouraged to pass large objects by value.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.