[Qt-interest] correct usage of qt smart pointers ?

Bence Magyar mw.mzperx at gmail.com
Tue May 10 12:41:00 CEST 2011


Anton is right.
I will explain it a bit more.

QSharedPointer work just like boost::tr1::shared_ptr.
To put it some other way: it works like a hardlink in a unix system.

So when you create the first shared pointer, your object will be created in
the background (some call it "inline factory") and the reference counter
will be set to 1.
Each time you return this value with your function as a QSharedPointer
object, the reference count will be incremented and each time one of these
objects destruct, the refcount will decrement.
When it's 0, your original object referenced by this QSharedPointer will be
destructed.

QWeakPointer is the same without the refcount, so it's not guaranteed if the
pointer is still valid when you try to use it. You cannot dereference it
directly but can keep track of your original object to check for example if
it's "still alive".

>From the Qt docu:
"Therefore, to access the pointer that QWeakPointer is tracking, you must
first promote it to
QSharedPointer<http://doc.qt.nokia.com/latest/qsharedpointer.html> and
verify if the resulting object is null or not.
QSharedPointer<http://doc.qt.nokia.com/latest/qsharedpointer.html>
guarantees
that the object isn't deleted, so if you obtain a non-null object, you may
use the pointer.
SeeQWeakPointer::toStrongRef<http://doc.qt.nokia.com/latest/qweakpointer.html#toStrongRef>()
for more an example."

http://doc.qt.nokia.com/latest/qweakpointer.html#details


Have fun :)

2011/5/10 Anton Chernov <mechernov at gmail.com>

> QWeakPointer are used only to track the object, not to work with.
>
> 2011/5/9 jjDaNiMoTh <jjdanimoth at gmail.com>
>
>> 2011/5/9 jjDaNiMoTh <jjdanimoth at gmail.com>:
>> > 2011/5/9 Matthias Pospiech <matthias.pospiech at gmx.de>:
>> >> I want to store derived classes pointer in a base class vector.
>> >> The saved pointer shall be accesible, but not deletable from outside
>> >
>> > When I was searching a thing (which was likely to yours) I
>> > encountered, on the Parashift's c++ faq, the following statement:
>> >
>> > "And nothing in C++ "prevents" people from doing the wrong thing" [1]
>> >
>> > Consider only to write a comment, like "Never delete" or "I will
>> > delete this in the future, don't care about that".
>> >
>> > [1]
>> http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.12
>> >
>>
>> And also (sry for double-posting) if you don't want modifications,
>> consider using right "const"  [1] word.
>>
>> [1] http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110510/eea7018e/attachment.html 


More information about the Qt-interest-old mailing list