【发布时间】:2017-10-02 17:30:55
【问题描述】:
我刚刚稳定了一个unique_pointer引用作为函数参数传递的情况。所以我看了一下,发现代码实际上是在编译和运行。
为什么会这样? 当您可以引用该指针时,该指针如何唯一?
这是我的例子:
class Foo{
public:
int bar{23};
};
void Bar(std::unique_ptr<Foo>& a_foo){
a_foo->bar = 42;
}
int main(int argc, char *argv[]){
auto foo{ std::make_unique<Foo>() };
Bar(foo);
std::cout << foo->bar << std::endl; //outputs 42
return 0;
}
【问题讨论】:
-
“唯一”是指“唯一所有权”。
-
做。你知道get方法吗?
标签: c++ c++11 c++14 unique-ptr