Memberwise Copying for Classes with Class Members

Suppose you use the String class, or, for that matter, the standard string class as a type for class members:

class Magazine{private:    String title;    string publisher;...};

String and string both use dynamic memory allocation. Does that mean you need to write a copy constructor and assignment operator for the Magazine class? No—at least not in itself. The default memberwise copying and assignment behavior does have some smarts. If you copy or assign one Magazine object to another, memberwise copying uses the copy constructors and assignment operators defined for the member types. That is, the String copy constructor will be used to copy the title member from one Magazine object to another, ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.