Article View: comp.std.c++
Article #3065Re: FW: Inherent C++ problem?
From: Etay Bogner
Date: Mon, 22 Jan 1996 17:52
Date: Mon, 22 Jan 1996 17:52
61 lines
2608 bytes
2608 bytes
In article <01BAE696.8C249300@dino.int.com>, Eugene Lazutkin
<eugene@int.com> wrote:
>> ----------
>> From: Max Motovilov[SMTP:max@int.com]
>> Sent: Friday, January 19, 1996 4:01 PM
>> To: 'Eugene Lazutkin'
>> Subject: (for comp.std.c++) Inherent C++ problem?
>>
>> It is all right by now. Consider the following use then:
>>
>> Complex a( 1, 0 ), b( 0, 1 );
>> Complex c( a+b );
>>
>> In the last line 3 objects of type Complex are constructed instead of 1,
>> namely:
>>
>> 1) Temporary Complex object constructed in 'operator +'
>> 2) Return value of 'operator +' copy-constructed from (1)
>> 3) 'c', copy-constructed from (2)
>>
For simplicity, let's write this as :
Complex callee() {
return Complex(0,0);
}
void caller() {
Complex a = callee(); // which is basically the same as a(callee())
}
There are two points of interest :
1) the "caller" function
2) the "callee" function
Let's see what the compilers knows at each point :
1) the compiler knows it's going to call a function the returns an object.
So it might make some space on the stack for the returned object and put a
pointer to this space somewhere in the argument list to the "callee()"
function.
2) the compiler know that this function is going to return an object, so
it expects to find a pointer to place the newly created in. it indeed
creates a new object but it directly creates it in the "callee()" stack
space.
So now the, your example is actually doing only one step :
operator+ create the newly created "a+b" object directly in c.
--
-- Etay Bogner,
-- Etay_Bogner@mail.stil.scitex.com,
-- Scitex Corp.
-- Israel.
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<Etay_Bogner-2101961132190001@metay.stil.scitex.com>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<01BAE696.8C249300@dino.int.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: Etay_Bogner@mail.stil.scitex.com (Etay Bogner) Newsgroups: comp.std.c++ Subject: Re: FW: Inherent C++ problem? Date: 22 Jan 1996 09:52:13 PST Organization: Scitex Corp. Approved: austern@isolde.mti.sgi.com Message-ID: <Etay_Bogner-2101961132190001@metay.stil.scitex.com> References: <01BAE696.8C249300@dino.int.com> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Sun, 21 Jan 1996 11:32:19 +0200 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMQPO5Ey4NqrwXLNJAQGtNwH9HTwdbR0yppid4lpgGv3bWV16vbGsAmM3 +iu3+IwNm7hcEJh/rI1J1vM2sFuq73WiVh48+k0EqcwgcOYNoou9UA== =z8VJ Originator: austern@isolde.mti.sgi.com