4.8. Implementing a Copy Assignment Operator

By default, when we assign one class object with another, as in

Triangular tri1( 8 ), tri2( 8, 9 ); 
tri1 = tri2; 

the data members of the class are copied in turn. In our example, _length, _beg_pos, and _next are copied from tri2 to tri1. This is called default memberwise copy.

In the case of the Triangular class, the default memberwise copy semantics are sufficient; there is nothing we need to do explicitly. In the case of the Section 4.2 Matrix class, however, the default memberwise behavior is not adequate. The reasons for this are discussed in the Default Memberwise Initialization subsection of Section 4.2.

The Matrix class requires both a copy constructor and a copy assignment operator. Here ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.