Previous |Next |

 

2    String Constructors and Destructor

  String::String(const char * Value)
{
  if( Value == NULL )   // Use "" if Value is NULL.
    {
      Get_Buffer(0);    // allocate storage for Buffer
      Buffer[0] = '\0'; // the built-in operator []
    }
  else
    {
      Get_Buffer( strlen( Value ) );  // strlen from <string.h>
      for(int i = 0; i < Buffer_Len; i++ )
          Buffer[i] = Value[i];
    }
}


QUESTION: What happens when the call is String x?

ANSWER:


This is the copy constructor for string.

String::String( const String & Value )
{
  Get_Buffer( strlen( Value.Buffer ) );
  for( int i = 0; i < Buffer_Len; i++ )
 {
    Buffer[ i ] = Value.Buffer[ i ];
 }
}
 

QUESTION: Why is it important to write this
copy constructor ourselves, and not rely on the compiler's copy constructor?

ANSWER:



 
Previous |Next |