【发布时间】:2012-02-16 23:12:20
【问题描述】:
我有一个带有sayHello() 方法的Foobar 类,该方法输出“你好!”。如果我写下面的代码
vector<unique_ptr<Foobar>> fooList;
fooList.emplace_back(new Foobar());
unique_ptr<Foobar> myFoo = move(fooList[0]);
unique_ptr<Foobar> myFoo2 = move(fooList[0]);
myFoo->sayHello();
myFoo2->sayHello();
cout << "vector size: " << fooList.size() << endl;
输出是:
Well hello there!
Well hello there!
vector size: 1
我很困惑为什么会这样。当我迈出第一步时,fooList[0] 不应该变为 null 吗?为什么myFoo2 有效?
Foobar 是这样的:
class Foobar
{
public:
Foobar(void) {};
virtual ~Foobar(void) {};
void sayHello() const {
cout << "Well hello there!" << endl;
};
};
【问题讨论】:
标签: c++ visual-studio-2010 stl move-semantics