Path: utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!sdd.hp.com!ucsd!rutgers!netnews.upenn.edu!chin From: chin@sg1.chem.upenn.edu (Chin Wu) Newsgroups: comp.lang.c++ Subject: return object Message-ID: Date: 7 Aug 90 20:25:27 GMT Sender: news@netnews.upenn.edu Distribution: comp Organization: University of Pennsylvania, Philadelphia, PA Lines: 34 Avoid extra constructor call when returning classes from functions. =================================================================== Consider a function `m ()' with a return value of class `X'. X m () { X b; b.a = 23; return b; } and the declaration X v = m (); What happens here is that `m ()' secretly has an argument, the address of the return value. At invocation, the address of enough space to hold `v' is sent in as the hidden argument. Then `b' is constructed and its `b' field is set to the value 23. Then an X(X&) constructor is applied to `b', with the hidden return value location as the target, so that `v' is now bound to the return value. But this is pretty wasteful. The local `b' is declared just to hold something that will be copied right out. While a compiler that combined an "elision" algorithm with interprocedural data flow analysis could conceivably eliminate all of this, it seems much more practical to allow programmers to assist the compiler in generating efficient code by somehow manipulating the return value explicitly, thus avoiding the local variable and X(X&) constructor all together. -------------------- Above is an extract from G++ info file. Is there any way in C++ you can avoid this problem? Any help will be appreciated. chin@sg1.chem.upenn.edu -- chin@sg1.chem.upenn.edu