std::shared_ptr<T>
之间有什么区别?和 std::shared_ptr<T const>
?
你什么时候会用一个和另一个?
最佳答案
shared_ptr<int>
是shared_ptr
到非常量int
.您可以修改 int 和shared_ptr
shared_ptr<const int>
是shared_ptr
到const int
.您不能修改const int
shared_ptr
指向,因为它是const
.但是你可以修改shared_ptr
本身(分配给它,调用其他非常量方法等)const shared_ptr<int>
是const shared_ptr
到非常量int
.您不能修改shared_ptr
(通过调用reset
或任何非常量方法),但您可以修改int
它指向const shared_ptr<const int>
是const shared_ptr
到const int
.你不能修改插孔。
关于C++:std::shared_ptr<T> 和 std::shared_ptr<T const> 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14824905/