我剛剛穩定的情況是一個unique_pointer引用作爲函數參數傳遞。所以我看了一下,發現代碼實際上是在編譯和運行。C++ 11/14獨特的指針不那麼獨特?
這是爲什麼呢? 當你可以引用該指針時,指針是如何唯一的?
這裏我舉的例子:
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方法嗎? – 2017-05-07 15:12:05