【发布时间】:2014-09-25 11:06:15
【问题描述】:
std::shared_ptr 和 std::unique_ptrs 函数 .get() 和 operator-> 是否完全相同?
或者和std::vectors .at()和operator[]有区别吗?
【问题讨论】:
标签: c++ c++11 stl operators smart-pointers
std::shared_ptr 和 std::unique_ptrs 函数 .get() 和 operator-> 是否完全相同?
或者和std::vectors .at()和operator[]有区别吗?
【问题讨论】:
标签: c++ c++11 stl operators smart-pointers
具有相同的行为(在这两种情况下,operator->() 都定义为返回 get()),但 operator->() 的前提条件是 get() 不得返回 0。
这意味着:
a.get(); // does not cause UB just because holds a null pointer
a.operator->(); // would cause UB if a.get() == 0
其中a 是std::unique_ptr 或std::shared_ptr。
【讨论】: