ycy@walt.cc.utexas.edu (Joseph Yip) (10/31/89)
Last week, our lecture was on operator overloading. However, we were
not able to code class assignment and addition like A = B + C + D without
losing chunk of memory.
String A, B, C, D;
...
String& String::operator+(String& S) {
String *tmp = new String;
...
}
By using temporary variable, the addition operation can be performed without
altering B, C, and D. Since the member function operator+() will be called
twice in A=B+C+D expression, two temporary variables will be created. The
problem is how to dispose the temporary variable which is created during
the first call to operator+() after B+C+D has finished.
A = B + C does not have this problem because after B + C operation, the
new temporary variable will be assigned to A.
Any better ideas? Thank you.
Joseph Yip