c++ - 你如何让 std::shared_ptr 不调用 delete()

标签 c++ c++11 shared-ptr

我有将 std::shared_ptr 作为参数的函数,所以我被迫使用 std::shared_ptr,但我传递给函数的对象不是动态分配的。如何将对象包装在 std::shared_ptr 中并让 std::shared_ptr 不对它调用 delete。

最佳答案

MyType t;
nasty_function(std::shared_ptr<MyType>(&t, [](MyType*){}));

关于c++ - 你如何让 std::shared_ptr 不调用 delete(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131877/

相关文章:

c++ - 如何在 Windows 上构建 paho mqtt c++

c++ - 调整大小时出现 std::vector 和内存错误

c++ - 插入无序容器的确定性

c++ - 有什么方法可以确定上下文是否允许使用 "this"?

c++ - iso 12.1 p5 中的第 4 个要点对我来说没有意义

c++ - 如何为成员创建 shared_ptr?

c++ - 任何代表有序元素对的 STL/boost 类型?

c++ - 对 push_front 属性有点困惑。 intList.push_front(2 * intList.back()) 究竟会做什么?

c++ - SWIG, boost 共享指针和继承

c++ - Xerces-c 和 shared_ptr