4.4. What Is the this Pointer?

We must implement a copy() member function that initializes one Triangular class object with another. For example, given

Triangular tr1( 8 ); 
Triangular tr2( 8, 9 ); 

the invocation of

tr1.copy( tr2); 

assigns tr1 the length and beginning position of tr2. In addition, copy() must return the class object that is the target of the copy. In our example, tr1 is both the target and the object that must be returned. How can we do that? For example, here is an implementation of copy():

Triangular& Triangular:: 
copy( const Triangular &rhs ) 
{ 
   _length = rhs._length; 
   _beg_pos = rhs._beg.pos; 
   _next = rhs._beg_pos-1; 

   return ??? what exactly ???; 
}; 

rhs is bound to tr2 in our example. In the assignment

 _length = rhs._length; ...

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.