Effective C++ by Scott Meyers (Short Summary)
  • Introduction
  • Chapter 1 - Accustoming Yourself to C++
    • Item 1 - View C++ as a federation of languages.
    • Item 2 - Prefer consts, enums, and inlines to #defines.
    • Item 3 - Use const whenever possible.
    • Item 4 - Make sure that objects are initialized before they’re used.
  • Chapter 2 - Constructors, Destructors, and Assignment Operators
    • Item 5 - Know what functions C++ silently writes a calls.
    • Item 6 - Explicitly disallow the use of compiler-generated functions you do not want.
    • Item 7 - Declare destructors virtual in polymorphic base classes.
    • Item 8 - Prevent exceptions from leaving destructors.
    • Item 9 - Never call virtual functions during construction or destruction.
    • Item 10 - Have assignment operators return a reference to *this.
    • Item 11 - Handle assignment to self in operator=.
    • Item 12 - Copy all parts of an object.
  • Chapter 3 - Resource Management
    • Item 13 - Use objects to manage resources.
    • Item 14 - Think carefully about copying behavior in resource-managing classes.
    • Item 15 - Provide access to raw resources in resource-managing classes.
    • Item 16 - Use the same form in corresponding uses of new and delete.
    • Item 17 - Store newed objects in smart pointers in standalone statements.
  • Chapter 4 - Designs and Declarations
    • Item 18 - Make interfaces easy to use correctly and hard to use incorrectly.
    • Item 19 - Treat class design as type design.
    • Item 20 - Prefer pass-by-reference-to-const to pass-by-value.
    • Item 21 - Don't try to return a reference when you must return an object.
    • Item 22 - Declare data members private.
    • Item 23 - Prefer non-member non-friend functions to member functions.
    • Item 24 - Declare non-member functions when type conversions should apply to all parameters.
    • Item 25 - Consider support for a non-throwing swap.
Powered by GitBook
On this page
  1. Chapter 2 - Constructors, Destructors, and Assignment Operators

Item 12 - Copy all parts of an object.

Consider a class representing customers, where the copying functions have been manually written so that calls to them are logged:

void logCall(const std::string& funcName);    //make a log entry
class Customer {
private:
   ...
   Customer (const Customer& rhs);
   Customer& operator= (const Customer& rhs);
   ...
private:
   std::string name;
};

Customer::Customer(const Customer& rhs)
: name(rhs.name)                // copy rhs's data
{
   logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs)
{
   logCall("Customer copy assignment operator");
   name = rhs.name;            // copy rhs's data
   return *this;
}

Everything here looks fine, and in fact everything is fine — untill another data member is added to Customer:

class Date { ... };            // for dates in times
class Customer {
public:
   ...
private:
   std::string name;
   Date lastTransaction;
};

At this point, the existing copying functions are performing a partial copy: they're copying the customer's name, but not its lastTransaction.

One of the most insidious ways this issue can arise is through inheritance. COnsider:

class PriorityCustomer: public Customer {
public:
   ...
   PriorityCustomer(const PriorityCustomer& rhs);
   PriorityCustomer& operator= (const PriorityCustomer& rhs);
   ...
private:
   int priority;
};

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: priority(rhs.priority)
{
   logCall("PriorityCustomer copy constructor");
}

PriorityCustomer& PriorityCustomer::operator= (const PriorityCustomer &rhs)
{
   logCall("PriorityCustomer copy assignment operator");
   priority = rhs.priority;
   return *this;
}

PriorityCustomer’s copying functions look like they’re copying everything in PriorityCustomer , but look again. Yes, they copy the data member that PriorityCustomer declares, but every PriorityCustomer also contains a copy of the data members it inherits from Customer , and those data members are not being copied at all! PriorityCustomer’s copy constructor specifies no arguments to be passed to its base class constructor (i.e., it makes no mention of Customer on its member initialization list), so the Customer part of the PriorityCustomer object will be initialized by the Customer constructor taking no arguments — by the default constructor. (Assuming it has one. If not, the code won’t compile.) That constructor will perform a default initialization for name and lastTransaction.

Any time you take it upon yourself to write copying functions for a derived class, you must take care to also copy the base class parts. Those parts are typically private, of course, so you can't access them directly. Instead, derived class copying functions must invoke their corresponding base class functions:

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: Customer(rhs),                // invoke base class copy ctor
   priority(rhs.priority)
{
   logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
   logCall("PriorityCustomer copy assignment operator");
   Customer::operator=(rhs);         // assign base class parts
   priority = rhs.priority;
   return *this;
}

The meaning of "copy all parts" in this Item's title should now be clear. When you're writing a copy function, be sure to

  • copy all local data members and

  • invoke the appropriate copying function in all base classes, too.

Note: If you find that your copy constructor and copy assignment operator have similar code bodies, eliminate the duplication by creating a third member function that both call. Such a function is typically private and is often named init. This strategy is safe, proven way to eliminate code duplication in copy constructor and copy assignment operators.

Things to Remember

  • Copying functions should be sure to copy all of an object's data members and all of its base class parts.

  • Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.

PreviousItem 11 - Handle assignment to self in operator=.NextChapter 3 - Resource Management

Last updated 3 years ago