[Scummvm-devel] Being memory & speed efficient when dealing with C++ objects
Philip Kendall
pak21 at srcf.ucam.org
Sun Jun 4 19:42:38 CEST 2006
On Sat, Jun 03, 2006 at 06:21:13PM +0200, Max Horn wrote:
> Hi there,
>
> the following might be old news to some of you, but maybe a few
> people out there can learn something from this, so I thought I post
> it here anyway.
>
> When dealing with objects in C++, one has to be careful if one wants
> to be speed and memory efficient. C++ has this habit of copying
> objects in places were you might not expect it. Especially if you are
> from a Java / Python / other GC collected background.
>
> Consider this statement:
> String myStr = someOtherString;
> This is in fact equivalent to
> String myStr;
> myStr = someOtherString;
> In other words, it will first create the string object myStr (on the
> stack). *Then* it will invoke the assignment operator, overriding the
> default value of myStr.
Are you sure about this? Stroustrup gives the example of
int main ()
{
string s = "Newton"; // string initalized (using copy constructor)
s = g(s)
}
(Section 11.7, "Essential Operators" in my edition). My very quick tests
with g++ would appear to confirm this behaviour.
Cheers,
Phil
--
"I don't want a used ride. I want my motorcycle." "It's just a machine."
"It's an extension of my soul... if there is such a thing."
Max and Original Cindy: Dark Angel: 411 on the DL
More information about the Scummvm-devel
mailing list