c++ - 带有 shared_ptr 的 RAII

标签 c++ shared-ptr raii

我想通过智能指针初始化一个类的两个实例:

    std::shared_ptr< myQueue > _pA ;
    std::shared_ptr< myQueue > _pB ;

    _pA.reset( new myQueue() ) ;
    _pB.reset( new myQueue() ) ;

我是否已使用上述重置函数初始化了两个不同的 myQueues 或同一对象上的两个指针?

最佳答案

假设您的 myQueue 类并不奇怪,是的,您将有两个独立的实例。

您可能还对 make_shared 感兴趣,这样你就可以这样写:

auto _pA = std::make_shared<myQueue>();
auto _pB = std::make_shared<myQueue>();

关于c++ - 带有 shared_ptr 的 RAII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475281/

相关文章:

c++ - RAII 多重构造函数

c++ - 是否支持 RAII?

c++ - 哪个 STL 容器最能满足这些需求?

c++ - 用于多态性的引用和指针?

c++ - 通过引用传递与通过 shared_ptr 传递

C++11 Shared Ptr,共享同一个引用计数器

c++ - RAII 线程安全 getter

c++ - 显示带有多个参数的 QMessageBox

c++ - 无法将正确的 lambda 传递给 emscripten_set_main_loop

c++ - shared_ptr 在 if 条件下如何工作