When I was reading:
What's the difference between assignment operator and copy constructor?
here: What's the difference between assignment operator and copy constructor?
The following example was shown:
A aa;
A a = aa; //copy constructor
vs:
A aa;
A a;
a = aa; // assignment operator
and my question is, why we need the assignment operator at all? I mean that it will be more efficient to use the copy constructor in this case while it does the same job.
could someone give a real-world example of the use of assignment operator where that can't be replaced by the copy constructor?
You need assignments when you want to change a value after it has already been constructed. For example:
std::mapstd::vectorThe list could really go on almost endlessly. But in your example it is true that one could simply avoid the use of an assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With