c++qtqsharedpointer

Does the number of references change when calling data() of a qsharedpointer


If I write this code:

QSharedPointer<int> ptr(new int());  

The number of references pointing to the integer is 1.
But when I call data() like this:

QSharedPointer<int> ptr(new int());   
int* ptr2 = ptr.data();  

Is the number of references 1 or 2 ?
Thanks for help.


Solution

  • It won't change. QSharedPointer only shares the pointer with QSharedPointer. It won't and can't share with raw pointer.

    QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

    And QSharedPointer::data() does nothing except

    Returns the value of the pointer referenced by this object.