More Books
C++ Gotchas: Avoiding Common Problems in Coding and Design
Main Page
Table of content
Copyright
Addison-Wesley Professional Computing Series
Preface
Acknowledgments
Chapter 1. Basics
Gotcha #1: Excessive Commenting
Gotcha #2: Magic Numbers
Gotcha #3: Global Variables
Gotcha #4: Failure to Distinguish Overloading from Default Initialization
Gotcha #5: Misunderstanding References
Gotcha #6: Misunderstanding Const
Gotcha #7: Ignorance of Base Language Subtleties
Gotcha #8: Failure to Distinguish Access and Visibility
Gotcha #9: Using Bad Language
Gotcha #10: Ignorance of Idiom
Gotcha #11: Unnecessary Cleverness
Gotcha #12: Adolescent Behavior
Chapter 2. Syntax
Gotcha #13: Array/Initializer Confusion
Gotcha #14: Evaluation Order Indecision
Gotcha #15: Precedence Problems
Gotcha #16: 'for' Statement Debacle
Gotcha #17: Maximal Munch Problems
Gotcha #18: Creative Declaration-Specifier Ordering
Gotcha #19: Function/Object Ambiguity
Gotcha #20: Migrating Type-Qualifiers
Gotcha #21: Self-Initialization
Gotcha #22: Static and Extern Types
Gotcha #23: Operator Function Lookup Anomaly
Gotcha #24: Operator '->' Subtleties
Chapter 3. The Preprocessor
Gotcha #25: '#define' Literals
Gotcha #26: '#define' Pseudofunctions
Gotcha #27: Overuse of '#if'
Gotcha #28: Side Effects in Assertions
Chapter 4. Conversions
Gotcha #29: Converting through 'void *'
Gotcha #30: Slicing
Gotcha #31: Misunderstanding Pointer-to-Const Conversion
Gotcha #32: Misunderstanding Pointer-to-Pointer-to-Const Conversion
Gotcha #33: Misunderstanding Pointer-to-Pointer-to-Base Conversion
Gotcha #34: Pointer-to-Multidimensional-Array Problems
Gotcha #35: Unchecked Downcasting
Gotcha #36: Misusing Conversion Operators
Gotcha #37: Unintended Constructor Conversion
Gotcha #38: Casting under Multiple Inheritance
Gotcha #39: Casting Incomplete Types
Gotcha #40: Old-Style Casts
Gotcha #41: Static Casts
Gotcha #42: Temporary Initialization of Formal Arguments
Gotcha #43: Temporary Lifetime
Gotcha #44: References and Temporaries
Gotcha #45: Ambiguity Failure of 'dynamic_cast'
Gotcha #46: Misunderstanding Contravariance
Chapter 5. Initialization
Gotcha #47: Assignment/Initialization Confusion
Gotcha #48: Improperly Scoped Variables
Gotcha #49: Failure to Appreciate C++'s Fixation on Copy Operations
Gotcha #50: Bitwise Copy of Class Objects
Gotcha #51: Confusing Initialization and Assignment in Constructors
Gotcha #52: Inconsistent Ordering of the Member Initialization List
Gotcha #53: Virtual Base Default Initialization
Gotcha #54: Copy Constructor Base Initialization
Gotcha #55: Runtime Static Initialization Order
Gotcha #56: Direct versus Copy Initialization
Gotcha #57: Direct Argument Initialization
Gotcha #58: Ignorance of the Return Value Optimizations
Gotcha #59: Initializing a Static Member in a Constructor
Chapter 6. Memory and Resource Management
Gotcha #60: Failure to Distinguish Scalar and Array Allocation
Gotcha #61: Checking for Allocation Failure
Gotcha #62: Replacing Global New and Delete
Gotcha #63: Confusing Scope and Activation of Member 'new' and 'delete'
Gotcha #64: Throwing String Literals
Gotcha #65: Improper Exception Mechanics
Gotcha #66: Abusing Local Addresses
Gotcha #67: Failure to Employ Resource Acquisition Is Initialization
Gotcha #68: Improper Use of 'auto_ptr'
Chapter 7. Polymorphism
Gotcha #69: Type Codes
Gotcha #70: Nonvirtual Base Class Destructor
Gotcha #71: Hiding Nonvirtual Functions
Gotcha #72: Making Template Methods Too Flexible
Gotcha #73: Overloading Virtual Functions
Gotcha #74: Virtual Functions with Default Argument Initializers
Gotcha #75: Calling Virtual Functions in Constructors and Destructors
Gotcha #76: Virtual Assignment
Gotcha #77: Failure to Distinguish among Overloading, Overriding, and Hiding
Gotcha #78: Failure to Grok Virtual Functions and Overriding
Gotcha #79: Dominance Issues
Chapter 8. Class Design
Gotcha #80: Get/Set Interfaces
Gotcha #81: Const and Reference Data Members
Gotcha #82: Not Understanding the Meaning of Const Member Functions
Gotcha #83: Failure to Distinguish Aggregation and Acquaintance
Gotcha #84: Improper Operator Overloading
Gotcha #85: Precedence and Overloading
Gotcha #86: Friend versus Member Operators
Gotcha #87: Problems with Increment and Decrement
Gotcha #88: Misunderstanding Templated Copy Operations
Chapter 9. Hierarchy Design
Gotcha #89: Arrays of Class Objects
Gotcha #90: Improper Container Substitutability
Gotcha #91: Failure to Understand Protected Access
Gotcha #92: Public Inheritance for Code Reuse
Gotcha #93: Concrete Public Base Classes
Gotcha #94: Failure to Employ Degenerate Hierarchies
Gotcha #95: Overuse of Inheritance
Gotcha #96: Type-Based Control Structures
Gotcha #97: Cosmic Hierarchies
Gotcha #98: Asking Personal Questions of an Object
Gotcha #99: Capability Queries
Bibliography

Gotcha #49: Failure to Appreciate C++'s Fixation on Copy Operations

C++ takes its copy operations seriously. Copy operations are extremely important in C++ programming, particularly copy operations of class objects. These operations are so important that if you don't provide them for a class, the compiler will. Sometimes, even if you try to provide these operations yourself, the compiler will push you aside and provide them anyway. Sometimes the compiler implements the operations correctly; sometimes it doesn't. That's why it's a good idea to know precisely what the compiler expects with respect to copy operations.

Note that copy assignment and copy construction (along with other constructors and the destructor) are not inherited from base classes. Therefore, every class defines its own copy operations.

The default implementation of copy construction is to perform a member-by-member initialization. Member-by-member initialization has nothing to do with C-style bitwise copying of structures. Consider a simple class implementation:

template <int maxlen> 
class NBString {
 public:
   explicit NBString( const char *name );
   // . . .
 private:
   std::string name_;
   size_t len_;
   char s_[maxlen];
};

Assuming no copy operations are defined, the compiler will write some for us implicitly. These will be defined as public, inline members.

NBString<32> a( "String 1" ); 
// . . .
NBString<32> b( a );

The implicit copy constructor will perform a member-by-member initialization, invoking string's copy constructor to initialize b.name_ with a.name_, b.len_ with a.len_, and the elements of b.s_ with those of a.s_. (Actually, in an inspired burst of weirdness, the standard specifies that "scalar" types such as predefined types, enums, and pointers will be assigned within the implicit copy constructor rather than initialized. The group mental processes of the standards committee that led to this definition are beyond my ken, but the result will be the same—for these scalar types, anyway—whether assignment or initialization is used.)

b = a; 

Similarly, the implicit copy assignment operator will perform a member-by-member assignment, invoking string's assignment operator to assign a.name_ to b.name_, a.len_ to b.len_, and the elements of a.s_ to the corresponding elements of b.s_.

These implicit definitions of the copy operations will provide correct, conventional copy semantics. (See also Gotcha #81.) However, consider a slightly different implementation of the named, bounded string class:

class NBString { 
 public:
   explicit NBString( const char *name, int maxlen = 32 )
       : name_(name), len_(0), maxlen_(maxlen),
         s_(new char[maxlen] )
       { s_[0] = '\0'; }
   ~NBString()
       { delete [] s_; }
   // . . .
 private:
   std::string name_;
   size_t len_;
   size_t maxlen_;
   char *s_;
};

The constructor now sets the maximum size of the string, and storage for the character string is no longer physically within the NBString object. Now the implicit, compiler-provided copy operations are incorrect:

NBString c( "String 2" ); 
NBString d( c );
NBString e( "String 3" );
e = c;

The implicit copy constructor sets the s_ members of both c and d to the same heap-allocated buffer. The buffer will suffer a double deletion when the destructors for d and then c attempt to delete the same memory. Similarly, when c is assigned to e, the memory to which e.s_ refers is left dangling when e.s_ is set to the same buffer as c.s_, with results similar to those above. (See also Gotcha #53 for a short discussion of the implications of compiler-generated copy assignment in the presence of virtual base classes.)

A correct implementation must take over from the compiler the task of writing the copy operations in their entirety:

class NBString { 
 public:
   // . . .
   NBString( const NBString & );
   NBString &operator =( const NBString & );
 private:
   std::string name_;
   size_t len_;
   size_t maxlen_;
   char *s_;
};
// . . .
NBString::NBString( const NBString &that )
   : name_(that.name_), len_(that.len_), maxlen_(that.maxlen_),
     s_(strcpy(new char[that.maxlen_], that.s_))
   {}
NBString &NBString::operator =( const NBString &rhs ) {
   if( this != &rhs ) {
       name_ = rhs.name_;
       char *temp = new char[rhs.maxlen_];
       len_ = rhs.len_;
       maxlen_ = rhs.maxlen_;
       delete [] s_;
       s_ = strcpy( temp, rhs.s_ );
   }
   return *this;
}

Every class designer must take careful consideration of copy operations. They must either be supplied explicitly (and maintained with every change to the class's implementation), be generated implicitly by the compiler (with this decision revisited with every change to the class's implementation), or be denied using the coding idiom below:

class NBString { 
 public:
   // . . .
 private:
   NBString( const NBString &);
   NBString &operator =( const NBString & );
   // . . .
};

Declaring—but not defining—private copy operations has the effect of "turning off" copying for the class. The compiler will not attempt to provide implicit versions of the operations, and most code will not have access to the private members. Any accidental copying performed by members or friends of the class will be caught as an undefined function at link time.

It's pretty much impossible to talk your way past the compiler on the subject of implementing copy operations. The code below shows three creative but futile attempts to defeat the compiler:

class Derived; 
class Base {
 public:
   Derived &operator =( const Derived & );
   virtual Base &operator =( const Base & );
};
class Derived : public Base {
 public:
   using Base::operator =; // hidden
   template <class T>
   Derived &operator =( const T & ); // not a copy assign
   Derived &operator =( const Base & ); // not a copy assign
};

We already know that copy operations aren't inherited, but the using-declaration that imports the rather accommodating nonvirtual base class assignment operator doesn't prevent the compiler from writing one either, and the compiler's implicit version hides the one imported explicitly. (Note also that the base class mentions the derived class explicitly, which is poor design. See Gotcha #69.)

The use of a template assignment member function doesn't help; template members are never used to implement copy operations (see Gotcha #88). The virtual copy assignment from the base class is overridden in the derived class, but the overriding derived class assignment operator is not a copy assignment (see Gotcha #76). The C++ language is insistent: either you write the copy operations or the compiler will, and no fooling around.