我想通过智能指针初始化一个类的两个实例:
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/