【发布时间】:2014-08-17 04:26:45
【问题描述】:
SomeClass a, b, c;
SomeClass foo();
SomeClass a = (b + c); //Where is the object (b + c) allocated?
SomeClass a = foo(); //Where is the returned value of foo() allocated?
我的猜测是它们是在堆上分配的,因为我读到临时对象在表达式(;)的末尾被销毁。
这对我来说很有意义,因为移动构造函数可以通过窃取堆上临时对象的指针来实现。
【问题讨论】:
-
它们被销毁的事实并没有说明它们是分配在堆上还是堆栈上。在堆栈上分配的对象也会在某些时候被销毁(即调用它们的析构函数)。
标签: c++ heap-memory move-semantics allocation stack-memory