Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Copy Constructor

The copy constructor for class T is a member function of the form T(T&) or T(T const&). The purpose of a copy constructor is to initialize a new object of a given class by copying an existing object of that class. Consider this sequence of declarations:

Complex c;
Complex d(c);
The variable c is initialized using the default constructor, whereas d is initialized by calling the copy constructor. In effect, the value of c is copied into d.

The compiler will generate its own copy constructor for any class in which no copy constructor has been defined by the programmer. The compiler generated copy constructor initializes the member variables of the class using their respective copy constructors.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.