【问题标题】:Is there a difference between .get() and -> with smart pointers?.get() 和 -> 与智能指针有区别吗?
【发布时间】:2014-09-25 11:06:15
【问题描述】:

std::shared_ptrstd::unique_ptrs 函数 .get()operator-> 是否完全相同?

或者和std::vectors .at()operator[]有区别吗?

【问题讨论】:

    标签: c++ c++11 stl operators smart-pointers


    【解决方案1】:

    具有相同的行为(在这两种情况下,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
    

    其中astd::unique_ptrstd::shared_ptr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      相关资源
      最近更新 更多