The treatment of deep vs. shallow copy in introductory C++ textbooks

  • Authors:
  • Stephen P. Carl

  • Affiliations:
  • Wright State University, Dayton, Ohio

  • Venue:
  • ACM SIGCSE Bulletin
  • Year:
  • 2003

Quantified Score

Hi-index 0.00

Visualization

Abstract

The default behavior of C++ compilers is to generate a destructor, copy constructor, and assignment operator for each class defined in source code, if that class does not provide such. The constructor and assignment operator generated perform a shallow copy of objects, which is incorrect behavior for most objects that contain pointers. Students must be taught to provide these operators, and to write the copy constructor and assignment operator such that they perform a deep copy of objects. A survey of introductory C++ texts shows that some authors do not explain this important topic in a consistent or thorough way. Any instructor beginning to teach C++ needs to understand this or face the prospect of long hours debugging what otherwise would seem to be a straightforward assignment.