How to make sure that all data members of a class have been copied in its copy constructor?

When a class has many data members, it is difficult to determine whether a data item is copied or not in its copy constructor.

Is there a solution?

+5
source share
3 answers

A simple solution is to make sure that each type of element is copyable and that there are, for example, no pointers to itself or other pointers or links that need to be fixed. Then the generated copy constructor is good enough.

+8
source

The best thing to do is rely on what the compiler has created and ensure that class members are copied accordingly.

You can do this explicitly with = default : available with C ++ 11.

+3
source

If you need to create a copy constructor, you can use some auxiliary structure for which you will not create a copy constructor (leave the default value) and save the data that should be copied to the field (by value) of this type. Then you need only one field for copying.

0
source

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


All Articles