| |
Overloading Operator + the Right Way (cont'd)
Step 3: Implementation
So far so good. We have chosen to use an extern friend function and return the result by value. Now it's time to implement the overloaded operator + function. Here again, we're facing a choice between two options.
Option 1: Intrusive Concatenation
Under the intrusive concatenation model, the operator + function measures the length of the arguments, copies the strings to a sufficiently large buffer and returns the result (for the sake of brevity, I ignored pathological conditions such as heap exhaustion):
// Option 1: intrusive concatenation
String operator + (const String & s1, const String s2)
{
int len=s1.size()+s2.size();
String result;
result.buf=new char[len+1];
strcpy(result.buf, s1.buf);
strcat(result.buf, s2.buf); // concatenate second string
return result;
}
Although this implementation works fine, there is a less intrusive and more flexible solution based on the overloaded += operator.
Option 2: Using Operator +=
Instead of accessing private data members, allocating memory and using C functions, our implementation can use the overloaded += operator:
// Option 2: using operator +=
String operator + (const String & s1, const String s2)
{
String result=s1;
result+=s2;
return result;
}
This implementation is easier to maintain and doesn't access nonpublic members of class String. In fact, we don't even need to declare it as a friend function. The implementation of operator += is left as an exercise for the reader.
Our discussion focused on a homemade String class because this is the most widely-used example of using the overloaded + operator. However, you can apply the guidelines and strategies that I have shown to any user-defined type. Overloading operator + correctly is based on these principles:
Make it symmetric
The return value and the operands' types should be the same
Return the result by value
Base the implementation on the overloaded += operator.
 |
Danny Kalev is a system analyst and software engineer with 13 years of experience, specializing in C++ and object-oriented analysis and design. He is a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999, ISBN: 0789720221). Check out the DevX review here. He can be reached at dannykk@inter.net.il.
|
|
Overloading the + operator for a user-defined type is a common programming task. However, since C++ offers several implementation approaches, it's relatively easy to make design mistakes that have adverse effects on code correctness, performance and compatibility with Standard Library components.

Analyze the characteristics of the built-in operator and try to imitate them in the corresponding overloaded operator.
Find Out More
All DevX 10-Minute Solutions for C/C++
DevX Tip: Guidelines for Overloading the + Operator
DevX Tip: What are Lvalues and Rvalues?
STL Library Programmer's Guide
 | TALK BACK |
Operator overloading was deliberately omitted from Java because its creators considered it a source of "unnecessary complication." However, C# does support operator overloading, just as does C++. What is your stance regarding operator overloading? Is it an indispensable feature or a source of "unnecessary complication," as Java designers claimed?
 |
|
|