Q1: Can you rely on a compiler to automatically generate data?
Yes (in your example). See C ++ 11 Standard (section 12) or the article Implicit Wont Go! (good chart near the end). To summarize (and simplify), all of the following special member functions will be automatically generated (implicitly declared and defined as default):
- Destructor - because you did not declare it.
- Copy Constructor - because you did not declare it with either MC or MAO.
- Copy assignment operator - because you did not declare it with either MC or MAO.
- Move the constructor - because you did not declare it with D, CC, CAO, or MAO.
- Move Assignment Operator - because you did not declare it, neither in D, CC, CAO and MC.
(I used ugly initials only to save list items one line at a time.) In addition to the βbecauseβ above, for all but Destructor, there is an additional restriction that the generated default values ββmust have, that is, all given members must be copied (for the Central Committee and the CAO) or movable (for the MS and MAO). (Actually, the exact rules are a bit more complicated, but I don't want to rephrase the standard here.)
Q2: Are auto-generated functions performed correctly?
Yes (in your example). All your data members (here plain int s) have the correct copy / move semantics (their copy / move constructors and assignment operators do the right thing, and those Rect generated for Rect will call them).
Q3: In any case, should they be determined manually?
I do not see this as an advantage (in your example) and potential problems (as in your example, see comments).
source share