ranson@cnetlu.UUCP (Ranson) (11/20/89)
When defining operators on objects of a class in C++ (e.g. + on matrices), there are currently two possibilities (leaving aside the equivalent using member functions, and assuming args passed by reference): matrix& operator+(matrix& x, matrix& y) { ... } matrix operator+(matrix& x, matrix& y) { ... } The first one looks promising, but since returning a reference to a local variable is an error, one has to allocate a new matrix for the result, and disposing of it afterwards may not be easy, especially in complex expressions (e.g. how does one dispose of the result of A+B in (A+B)+C ?). Stroustrup recommends using the second form, where intermediate results will be dealt with automatically by C++, but this also means that the result needs to be copied for each operation. As I understand it, an expression like: X = (A+B)+C; is translated into: matrix U; U = operator+(A,B); X = operator+(U,C); Why not allow a third form, where the caller would pass as a third argument a reference to the place where the result should be put? void operator+(matrix& x, matrix& y, matrix& result) { ... } The previous example would translate into: matrix U; operator+(A,B,U); operator+(U,C,X); and no copying would be necessary. Am I missing something obvious? Daniel Ranson X400: ranson@lannion.cnet.fr uucp: ranson@cnetlu.fr, or ...!mcvax!inria!cnetlu!ranson
jimad@microsoft.UUCP (Jim Adcock) (11/28/89)
In article <1253@cnetlu.UUCP> ranson@cnetlu.UUCP (Ranson) writes: ... >matrix operator+(matrix& x, matrix& y) { ... } >Stroustrup recommends using <the above> form, where intermediate results >will be dealt with automatically by C++, but this also means that the >result needs to be copied for each operation. As I understand it, an >expression like: X = (A+B)+C; is translated into: > matrix U; > U = operator+(A,B); > X = operator+(U,C); > >Why not allow a third form, where the caller would pass as a third argument >a reference to the place where the result should be put? > >void operator+(matrix& x, matrix& y, matrix& result) { ... } > >The previous example would translate into: > matrix U; > operator+(A,B,U); > operator+(U,C,X); >and no copying would be necessary. Am I missing something obvious? The C++ compilers I am familiar with do not necessarily work as you assume. If I declare as follows: matrix(const matrix&); void friend operator=(matrix&, const matrix&); matrix friend operator+(const matrix& a, const matrix& b); ...then my 1.2 compatible compiler does what you want -- I can write: matrix D = A + B + C; and no matrix copying will be done. BUT NOTE: This is not the same as if you write: D = A + B + C; where D was declared previously. When one writes "matrix D =" one is telling the compiler to *construct* a new matrix out of the results of "A + B + C". Whereas when one writes "D = " you tell the compiler you want to have D *assigned* the value of some matrix -- but what matrix? -- one constructed from "A + B + C" ---- sooo you force the compiler to introduce a temporary that then can be copied into D: D = A + B + C; means: D = (matrix Tmp(A + B + C)); In C++ construction vs assignment are very much *not* the same thing. Unfortunately, a plethora of syntaxes (syntacti?) for construction vs assignment confuses the issue. Even if one does not optimally use construction verses assignment, C++ compilers can avoid most of the unnecessary copying by creating op+ in the three parameter form as you suggest.