简体   繁体   中英

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.

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.

Calling data() does not change reference count. In fact, it doesn't really do anything. Its implementation is just this

inline T *data() const { return value; }

This is taken directly from Qt sources .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM