Qt里的智能指针

一片文章里讲到Qt里众多的智能指针: 1. QPointer (4.0) 2. QSharedDataPointer (4.0) 3. QExplicitlySharedDataPointer (4.3/4.4) 4. QtPatternist::AutoPtr (internal class, 4.4) 5. QSharedPointer (4.5) 6. QWeakPointer (4.5) 7. QGuard (internal class, 4.6) 8. QScopedPointer (4.6) 废话一句,括号里是Qt版本。 最后的总结是这样的 So Qt has too many smart pointer classes. Or does it? In fact, Qt has only these pointer classes if you exclude the internal classes and you deprecate QPointer: Class Description QSharedDataPointer / QExplicitlySharedDataPointer Implements sharing of data (not of pointers), implicitly and explicitly, respectively QSharedPointer Implements reference-counted strong sharing of pointers QWeakPointer Implements reference-counted weak sharing of pointers QScopedPointer / QScopedArrayPointer Implements non-reference-counted strong pointer wrapper (QSharedPointer’s little brother) Update 1: QExplicitlySharedDataPointer can be used to implement reference-counted sharing of pointers when the target class includes the reference counter (similar to boost::intrusive_ptr) Update 2: QScopedPointer is really based on the API of boost::scoped_ptr (but is not a copy); QSharedPointer and QWeakPointer were developed from the scratch. 关于智能指针的sharepointer和sharedata,strong ref和 weak ref,有几段介绍,吾还需要多读几遍. Shared pointer versus shared data First, let’s get one thing straight: there’s a difference between sharing pointers and sharing data. When you share pointers, the value of the pointer and its lifetime is protected by the smart pointer class. In other words, the pointer is the invariant. However, the object that the pointer is pointing to is completely outside its control. We don’t know if the object is copiable or not, if it’s assignable or not. Now, sharing of data involves the smart pointer class knowing something about the data being shared. In fact, the whole point is that the data is being shared and we don’t care how. The fact that pointers are being used to share the data is irrelevant at this point. For example, you don’t really care how Qt tool classes are implicitly shared, do you? What matters to you is that they are shared (thus reducing memory consumption) and that they work as if they weren’t. Strong versus weak pointer referencing The difference between a strong and a weak reference is whether the existence of the smart pointer class on a given pointer guarantees that the object will not get deleted. In other words, if you have this smart pointer, are you sure that this will always remain valid (provided, of course, everyone is playing by the same rules)? Some of the pointer classes above don’t guarantee that. If they don’t guarantee that the object remains valid, their main purpose in life is to tell you whether the object has been deleted already or not. Some classes may provide an additional feature that allows you to promote a weak pointer to a strong one, thus guaranteeing that it won’t get deleted anymore.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值