Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!ut-emx!walt.cc.utexas.edu!ycy From: ycy@walt.cc.utexas.edu (Joseph Yip) Newsgroups: comp.lang.c++ Subject: How to do A = B + C + D Message-ID: <20277@ut-emx.UUCP> Date: 30 Oct 89 20:26:48 GMT Sender: news@ut-emx.UUCP Reply-To: ycy@walt.cc.utexas.edu (Joseph Yip) Distribution: usa Organization: The University of Texas at Austin, Austin, Texas Lines: 25 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