Home > other >  cannot convert argument 1 from 'std::shared_ptr<QtNodes::DataFlowGraphicsScene>' to
cannot convert argument 1 from 'std::shared_ptr<QtNodes::DataFlowGraphicsScene>' to

Time:02-05

mGraphicsView = std::make_shared<QtNodes::GraphicsView>(mGraphicsScene, parent);

The constructor takes a QtNodes::BasicGraphicsScene* as an argument, and I have an std::shared_ptr for that object. If I try to pass the shared pointer object directly as shown above, I get the following error:

cannot convert argument 1 from 'std::shared_ptr<QtNodes::DataFlowGraphicsScene>' to 'QtNodes::BasicGraphicsScene *'

Now, If I use the .get() function, It does work, but since I am passing the raw pointer without calling the copy constructor, the reference count in my shared_ptr will not increase. Therefore, the object might get deleted while still in use.

How do I pass my object pointer while also updating the reference counter.

CodePudding user response:

You don't.

You have two options to keep the object alive while the consumer is processing it:

  1. The caller takes care that the object stays around long enough.

  2. The consumer accepts a std::shared_ptr instead of a raw pointer.

In a case where the first option is not simple to achieve and there is already a std::shared_ptr, the second option is clearly preferred.

  • Related