Pregunta

I want to initialize two instances of a classes through smart pointers:

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

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

Do I have initialized with the above reset function two different myQueues or just two pointers on the same object?

¿Fue útil?

Solución

Assuming your myQueue class isn't weird, yes, you will have two separate instances.

You might also be interested in make_shared, which would let you write it like this:

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

Otros consejos

Initializing the two shared_ptr with two pointers to the same object will result in a double delete. You don't ever want to do it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top