C++:std::shared_ptr<T> 和 std::shared_ptr<T const> 有什么区别?

标签 c++ shared-ptr

std::shared_ptr<T> 之间有什么区别?和 std::shared_ptr<T const>

你什么时候会用一个和另一个?

最佳答案

  • shared_ptr<int> shared_ptr到非常量 int .您可以修改 int 和 shared_ptr

  • shared_ptr<const int> shared_ptrconst 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_ptrconst int .你不能修改插孔。

关于C++:std::shared_ptr<T> 和 std::shared_ptr<T const> 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14824905/

相关文章:

c++ - 如何在终止前强制输入?

c++ - GCC pragma 在源文件中添加/删除编译器选项

使用 shared_ptr 作为参数时的 C++/CLI "could not import member"警告

c++ - 使用 std::shared_ptr 检测循环引用

c++ - 在 shared_ptr 上调用方法时如何避免段错误?

c++ - 为什么 shared_ptr<T> 期望在 T 中复制/移动构造函数?

c++ - 用 C++ 实现图的想法

c++ - 当没有数据可读时,读取套接字会给出 errno EAGAIN,而且如果我断开网络连接

c++ - 将 cv::Mat 转换为 Magick::Image

android - 使用任意 Boost 库进行奇怪的 NDK 编译