The Copy Constructor and When You Need It

There is one special form of constructor with arguments: the copy constructor (see Listing 17.8).

This is used so that you can create an object whose state is exactly the same as the state of another object of the same class.

Listing 17.8. A Copy Constructor in aSAMSAccumulator
  1: class aSAMSAccumulator
  2: {
  3:    private:
  4:
  5:       float myAccumulator;
  6:
  7:    public:
  8:
  9:       enum anOperator
               {add,subtract,multiply,divide,query,reset} ;
 10:
 11:       aSAMSAccumulator(void);
 12:       aSAMSAccumulator(float theInitialAccumulatorValue);
*13:       aSAMSAccumulator
               (aSAMSAccumulator theOtherAccumulator);
 14:
 15:       virtual ~aSAMSAccumulator(void);
 16:
 17:       void Accumulate
               (char theOperator,float theOperand = 0);
 18: } ;

aSAMSAccumulator ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.