Are there situations where it would be nice to write your own copy constructor, but not your own assignment operator?

The question is pretty clear. If you need to create one of them, does that mean you also have to create the second?

+5
source share
2 answers

The usual need to write as indicated in the comments above.

However, you can imagine other needs that fit your question. For example, if you want to count the number of objects in your program. The copy constructor will have to increment the counter, while assignment does not change the number of objects, and therefore will be executed by default.

+2
source

If it makes no sense to assign an object to a specific type, you do not write / delete the assingnment op option. It may still make sense to have a copy of ctor, though.

A technical example is a class with reference elements. It is possible to create a copy, the copy destination is not the same as you cannot change what the ref members indicate.

+1
source

Source: https://habr.com/ru/post/1205531/


All Articles