【发布时间】:2014-04-22 15:48:56
【问题描述】:
c++ STL::list 的 push_back 函数将参数作为 pass-by-value 。但是当参数超出范围时仍然能够访问列表的元素。
typedef std::list<MyObject> mylist;
void function1()
{
MyObject obj;<<<< local scope
...
mylist.push_back(obj);
}
void function2()
{
//On Iterating the list "mylist" able to access objs in the list properly even though the scope of obj is lost in function1.
}
【问题讨论】:
-
Google 应该遥遥领先 Stack Overflow 出现在您的“从哪里获得帮助”的优先列表中。但是,既然您询问了 STL,no, it's passed by const reference。 standard library 也是如此。
-
对不起可能是问题不完整!我很困惑,因为原型正在引用,但我仍然能够访问列表元素,而参数失去了它们的范围!