开发者

What is temporary allocation?

开发者 https://www.devze.com 2023-01-08 08:19 出处:网络
In C++, what is temporary allocation and when is it used? Does such a thing even exist? It was mentione开发者_C百科d in the TA\'s course notes, but I couldn\'t find any info about it...When people say

In C++, what is temporary allocation and when is it used? Does such a thing even exist? It was mentione开发者_C百科d in the TA's course notes, but I couldn't find any info about it...


When people say "temporaries" they often refer to rvalues. That is objects created and not bound to a variable name, thus not living outside the current statement. IE:

int foo()
{
     Do( Object() );
}

The created Object() is an rvalue which you may hear referred to as a temporary.


I suspect that your TA may have been referring to objects without a name created during the evaluation of an expression.

SomeClass x(1), y(2), z(3);
SomeClass t = x + y + z;

The expression x + y + z invokes the operator+() twice; the result of the first is a temporary allocation (the result of the second initializes t).

0

精彩评论

暂无评论...
验证码 换一张
取 消