[英]C++: make unique_ptr null by reference to unique_ptr object
This might be a stupid question, but can I make unique_ptr null by reference to unique_ptr object?这可能是一个愚蠢的问题,但我可以通过引用 unique_ptr object 来制作 unique_ptr null 吗?
#include<iostream>
#include<memory>
int main()
{
std::unique_ptr<int> a = std::make_unique<int>(2);
int& b = *a.get();
// use "b" to make "a" nullptr
return 0;
}
No. There is no way to do that.不,没有办法做到这一点。 An int
is just an int
and a reference to an int
is just a reference to an int
. int
只是一个int
并且对int
的引用只是对int
的引用。 It does not know about pointers that hold its address.它不知道保存其地址的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.