简体   繁体   English

调用qsharedpointer的data()时引用的数量是否改变

[英]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. 指向整数的引用数为1。
But when I call data() like this: 但是当我这样调用data()时:

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

Is the number of references 1 or 2 ? 参考编号是1还是2?
Thanks for help. 感谢帮助。

It won't change. 它不会改变。 QSharedPointer only shares the pointer with QSharedPointer . QSharedPointer仅与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. 如果没有其他QSharedPointer对象引用它,则QSharedPointer将在其超出范围时删除它持有的指针。

And QSharedPointer::data() does nothing except QSharedPointer::data()除了什么都不做

Returns the value of the pointer referenced by this object. 返回此对象引用的指针的值。

Calling data() does not change reference count. 调用data()不会更改引用计数。 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 . 这直接取自Qt来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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