4
From what I understand, the Qsharedpointer is a smart Pointer, ie, a pointer that offers automatic garbage collection.
If possible I would like a code that better explains this, including when I should use it.
4
From what I understand, the Qsharedpointer is a smart Pointer, ie, a pointer that offers automatic garbage collection.
If possible I would like a code that better explains this, including when I should use it.
3
After much reading I could understand. But it is necessary to know what is a Qpointer.
According to QT documentation:
The Qpointer class is a model class that provides pointers reserved to Qobject. A saved pointer, Qpointer , behaves like a normal, C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "hanging pointers" in such cases). T has to be a subclass of Qobject.
Example:
QPointer<QLabel> label = new QLabel;
label->setText("&Status:");
...
if (label)
label->show();
If Qlabel is deleted, however, the label variable will get 0 instead of an invalid address, and the last line will never run. The functions and operators available with a Qpointer are the same as those available with a normal pointer, except for the arithmetic pointer operators (+, -, + + and -), which are normally used only with object matrices.
Now Qsharedpointer:
The Qsharedpointer class holds a strong reference to a shared pointer. Qsharedpointer is an automatic pointer, shared in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness. Qsharedpointer will delete the pointer is holding when it exits the scope, since no other Qsharedpointer objects are reference to it. A Qsharedpointer object can be created from a normal pointer, another Qsharedpointer object or by promoting a Qweakpointer object to a strong reference.
Obs.: The main objective of the constness is to provide documentation and avoid programming errors. Const lets you make it clear to yourself and others that something should not be changed.
Knowing this, we can then take into consideration this posting: https://stackoverflow.com/questions/22304118/what-is-the-difference-between-qpointer-qsharedpointer-and-qweakpointer-classes
Qpointer can only point to Qobject cases. It will be set automatically to nullptr if the pointer to object is destroyed. It is a specialized weak pointer for Qobject. Consider this fragment:
QObject * obj = new QObject;
QPointer <QObject> pObj (obj);
delete obj;
Q_ASSERT (pObj.isNull ()); //PObj será agora nullptr
Qsharedpointer A countered-reference pointer. The actual object will only be deleted when all shared pointers are destroyed.Equivalent to
std::shared_ptr.
int * pi = new int;
QSharedPointer <int> PI1 (pi);
QSharedPointer <int> PI2 = pi1;
pI1.clear (); //PI2 ainda está apontando para o pi, por isso não é eliminado
pI2.clear (); // Não há ponteiros compartilhados mais, PI é eliminado.
Note that while there is a shared pointer, the object will not be deleted!
Browser other questions tagged c++ qt pointer
You are not signed in. Login or sign up in order to post.