I'm new to C++ programming and have
a question about a line of code I often see. Classes that overload
the operator=() function always include the code
if (*this != other) { /* perform assignment */ }
Why do these functions need these lines of code? Is this simply
to enhance performance?
Roger Johnson
Fostoria, Ohio
Roger, you're partially correct: These lines of code save the time it would take to assign the this object to the other object. The conditional statement allows the assignment to take place only if the two objects are different.
However, there's a much more important reason why operator=() functions use the conditional statement. To understand why, let's take a look at an example class.
Our example, the String class, is the typical string-handling
class you see when you're first learning C++. The String
class uses dynamic memory allocation to set aside space for its
string data. Figure A shows a typical constructor for the String
class.
String::String(const char *aPtr) { if (aPtr == 0) aPtr = ""; len = strlen(aPtr) + 1; theString = new char[len]; CHECK(theString != 0); strcpy(theString, aPtr); }
As you can see, the String class uses the new operator to set aside enough memory for its string data. Therefore, each String object consumes only enough space to store its ASCII string.
Now let's take a look at the typical String class
operator=() function, which appears in Figure B. We've
modified the operator=() function slightly to demonstrate
our point.
String& String::operator =(const String& sourceString) { if (*this != sourceString) { if (len != sourceString.len) { delete theString; len = sourceString.len; theString = new char[len]; CHECK(theString != 0); } strcpy(theString, sourceString.theString); } return *this; }
The String class's operator=() function has one job: to assign the data in one String object to that of another. To do so, the function frees the memory of the receiving string, allocates memory for the new string, and copies the data from the source string.
The reason for the conditional (*this != other) statement is this: If the receiving and source String objects are the same object, the function shouldn't free receiving memory at all. If the operator=() function did free the receiving String object's memory, it would also free the source String object's memory. Once that happened, the source object's memory would no longer be allocated for that particular object and could be used by any other dynamically allocated memory object.
Therefore, without the conditional (*this != other) statement,
operator=() functions would free memory they shouldn't.
You'll see this line of code in any operator=()
function where the class dynamically allocates memory for a data
member.
Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.