Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QSharedPointer Class Reference

The QSharedPointer class holds a strong reference to a shared pointer More...

 #include <QSharedPointer>

Note: All functions in this class are reentrant.

This class was introduced in Qt 4.5.

Public Functions

QSharedPointer ()
QSharedPointer ( T * ptr )
QSharedPointer ( T * ptr, Deleter deleter )
QSharedPointer ( const QSharedPointer<T> & other )
QSharedPointer ( const QWeakPointer<T> & other )
~QSharedPointer ()
void clear ()
QSharedPointer<X> constCast () const
T * data () const
QSharedPointer<X> dynamicCast () const
bool isNull () const
QSharedPointer<X> objectCast () const
QSharedPointer<X> staticCast () const
QWeakPointer<T> toWeakRef () const
operator bool () const
bool operator! () const
T & operator* () const
T * operator-> () const
QSharedPointer<T> & operator= ( const QSharedPointer<T> & other )
QSharedPointer<T> & operator= ( const QWeakPointer<T> & other )

Related Non-Members

QSharedPointer<X> qSharedPointerCast ( const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerConstCast ( const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerDynamicCast ( const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerObjectCast ( const QSharedPointer<T> & other )
bool operator!= ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )
bool operator!= ( const QSharedPointer<T> & ptr1, const X * ptr2 )
bool operator!= ( const T * ptr1, const QSharedPointer<X> & ptr2 )
bool operator== ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )
bool operator== ( const QSharedPointer<T> & ptr1, const X * ptr2 )
bool operator== ( const T * ptr1, const QSharedPointer<X> & ptr2 )

Detailed Description

The QSharedPointer class holds a strong reference to a shared pointer

The QSharedPointer is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness.

QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

A QSharedPointer object can be created from a normal pointer, another QSharedPointer object or by promoting a QWeakPointer object to a strong reference.

Thread-Safety

QSharedPointer and QWeakPointer are thread-safe and operate atomically on the pointer value. Different threads can also access the QSharedPointer or QWeakPointer pointing to the same object at the same time without need for locking mechanisms.

It should be noted that, while the pointer value can be accessed in this manner, QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. Thread-safety and reentrancy rules for that object still apply.

Other Pointer Classes

Qt also provides two other pointer wrapper classes: QPointer and QSharedDataPointer. They are incompatible with one another, since each has its very different use case.

QSharedPointer holds a shared pointer by means of an external reference count (i.e., a reference counter placed outside the object). Like its name indicates, the pointer value is shared among all instances of QSharedPointer and QWeakPointer. The contents of the object pointed to by the pointer should not considered shared, however: there is only one object. For that reason, QSharedPointer does not provide a way to detach or make copies of the pointed object.

QSharedDataPointer, on the other hand, holds a pointer to shared data (i.e., a class derived from QSharedData). It does so by means of an internal reference count, placed in the QSharedData base class. This class can, therefore, detach based on the type of access made to the data being guarded: if it's a non-const access, it creates a copy atomically for the operation to complete.

QExplicitlySharedDataPointer is a variant of QSharedDataPointer, except that it only detaches if QExplicitlySharedDataPointer::detach() is explicitly called (hence the name).

QScopedPointer simply holds a pointer to a heap allocated object and deletes it in its destructor. This class is useful when an object needs to be heap allocated and deleted, but no more. QScopedPointer is lightweight, it makes no use of additional structure or reference counting.

Finally, QPointer holds a pointer to a QObject-derived object, but it does so weakly. QPointer can be replaced by QWeakPointer in almost all cases, since they have the same functionality. See QWeakPointer#tracking-qobject for more information.

Optional pointer tracking

A feature of QSharedPointer that can be enabled at compile-time for debugging purposes is a pointer tracking mechanism. When enabled, QSharedPointer registers in a global set all the pointers that it tracks. This allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects.

This function is enabled by defining the QT_SHAREDPOINTER_TRACK_POINTERS macro before including the QSharedPointer header.

It is safe to use this feature even with code compiled without the feature. QSharedPointer will ensure that the pointer is removed from the tracker even from code compiled without pointer tracking.

Note, however, that the pointer tracking feature has limitations on multiple- or virtual-inheritance (that is, in cases where two different pointer addresses can refer to the same object). In that case, if a pointer is cast to a different type and its value changes, QSharedPointer's pointer tracking mechanism may fail to detect that the object being tracked is the same.

See also QSharedDataPointer, QWeakPointer, and QScopedPointer.

Member Function Documentation

QSharedPointer::QSharedPointer ()

Creates a QSharedPointer that points to null (0).

QSharedPointer::QSharedPointer ( T * ptr )

Creates a QSharedPointer that points to ptr. The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.

QSharedPointer::QSharedPointer ( T * ptr, Deleter deleter )

Creates a QSharedPointer that points to ptr. The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.

The deleter parameter specifies the custom deleter for this object. The custom deleter is called when the strong reference count drops to 0 instead of the operator delete(). This is useful, for instance, for calling deleteLater() in a QObject instead:

 static void doDeleteLater(MyObject *obj)
 {
     obj->deleteLater();
 }

 void otherFunction()
 {
     QSharedPointer<MyObject> obj =
         QSharedPointer<MyObject>(new MyObject, doDeleteLater);

     // continue using obj
     obj.clear();    // calls obj->deleteLater();
 }

It is also possible to specify a member function directly, as in:

     QSharedPointer<MyObject> obj =
         QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);

See also clear().

QSharedPointer::QSharedPointer ( const QSharedPointer<T> & other )

Creates a QSharedPointer object that shares other's pointer.

If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.

QSharedPointer::QSharedPointer ( const QWeakPointer<T> & other )

Creates a QSharedPointer by promoting the weak reference other to strong reference and sharing its pointer.

If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.

See also QWeakPointer::toStrongRef().

QSharedPointer::~QSharedPointer ()

Destroys this QSharedPointer object. If it is the last reference to the pointer stored, this will delete the pointer as well.

void QSharedPointer::clear ()

Clears this QSharedPointer object, dropping the reference that it may have had to the pointer. If this was the last reference, then the pointer itself will be deleted.

QSharedPointer<X> QSharedPointer::constCast () const

Performs a const_cast from this pointer's type to X and returns a QSharedPointer that shares the reference. This function can be used for up- and for down-casting, but is more useful for up-casting.

See also isNull() and qSharedPointerConstCast().

T * QSharedPointer::data () const

Returns the value of the pointer referenced by this object.

Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.

QSharedPointer<X> QSharedPointer::dynamicCast () const

Performs a dynamic cast from this pointer's type to X and returns a QSharedPointer that shares the reference. If this function is used to up-cast, then QSharedPointer will perform a dynamic_cast, which means that if the object being pointed by this QSharedPointer is not of type X, the returned object will be null.

Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.

See also qSharedPointerDynamicCast().

bool QSharedPointer::isNull () const

Returns true if this object is holding a reference to a null pointer.

QSharedPointer<X> QSharedPointer::objectCast () const

Performs a qobject_cast() from this pointer's type to X and returns a QSharedPointer that shares the reference. If this function is used to up-cast, then QSharedPointer will perform a qobject_cast, which means that if the object being pointed by this QSharedPointer is not of type X, the returned object will be null.

Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.

This function was introduced in Qt 4.6.

See also qSharedPointerObjectCast().

QSharedPointer<X> QSharedPointer::staticCast () const

Performs a static cast from this pointer's type to X and returns a QSharedPointer that shares the reference. This function can be used for up- and for down-casting, but is more useful for up-casting.

Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.

See also dynamicCast(), constCast(), and qSharedPointerCast().

QWeakPointer<T> QSharedPointer::toWeakRef () const

Returns a weak reference object that shares the pointer referenced by this object.

See also QWeakPointer::QWeakPointer().

QSharedPointer::operator bool () const

Returns true if this object is not null. This function is suitable for use in if-constructs, like:

 if (sharedptr) { ... }

See also isNull().

bool QSharedPointer::operator! () const

Returns true if this object is null. This function is suitable for use in if-constructs, like:

 if (!sharedptr) { ... }

See also isNull().

T & QSharedPointer::operator* () const

Provides access to the shared pointer's members.

See also isNull().

T * QSharedPointer::operator-> () const

Provides access to the shared pointer's members.

See also isNull().

QSharedPointer<T> & QSharedPointer::operator= ( const QSharedPointer<T> & other )

Makes this object share other's pointer. The current pointer reference is discarded and, if it was the last, the pointer will be deleted.

If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.

QSharedPointer<T> & QSharedPointer::operator= ( const QWeakPointer<T> & other )

Promotes other to a strong reference and makes this object share a reference to the pointer referenced by it. The current pointer reference is discarded and, if it was the last, the pointer will be deleted.

If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.

Related Non-Members

QSharedPointer<X> qSharedPointerCast ( const QSharedPointer<T> & other )

Returns a shared pointer to the pointer held by other, cast to type X. The types T and X must belong to one hierarchy for the static_cast to succeed.

Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

See also QSharedPointer::staticCast(), qSharedPointerDynamicCast(), and qSharedPointerConstCast().

QSharedPointer<X> qSharedPointerConstCast ( const QSharedPointer<T> & other )

Returns a shared pointer to the pointer held by other, cast to type X. The types T and X must belong to one hierarchy for the const_cast to succeed. The const and volatile differences between T and X are ignored.

See also QSharedPointer::constCast(), qSharedPointerCast(), and qSharedPointerDynamicCast().

QSharedPointer<X> qSharedPointerDynamicCast ( const QSharedPointer<T> & other )

Returns a shared pointer to the pointer held by other, using a dynamic cast to type X to obtain an internal pointer of the appropriate type. If the dynamic_cast fails, the object returned will be null.

Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

See also QSharedPointer::dynamicCast(), qSharedPointerCast(), and qSharedPointerConstCast().

QSharedPointer<X> qSharedPointerObjectCast ( const QSharedPointer<T> & other )

The qSharedPointerObjectCast function is for casting a shared pointer.

Returns a shared pointer to the pointer held by other, using a qobject_cast() to type X to obtain an internal pointer of the appropriate type. If the qobject_cast fails, the object returned will be null.

Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

This function was introduced in Qt 4.6.

See also QSharedPointer::objectCast(), qSharedPointerCast(), and qSharedPointerConstCast().

bool operator!= ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )

Returns true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.

bool operator!= ( const QSharedPointer<T> & ptr1, const X * ptr2 )

Returns true if the pointer referenced by ptr1 is not the same pointer as ptr2.

If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.

bool operator!= ( const T * ptr1, const QSharedPointer<X> & ptr2 )

Returns true if the pointer ptr1 is not the same pointer as that referenced by ptr2.

If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.

bool operator== ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )

Returns true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.

bool operator== ( const QSharedPointer<T> & ptr1, const X * ptr2 )

Returns true if the pointer referenced by ptr1 is the same pointer as ptr2.

If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.

bool operator== ( const T * ptr1, const QSharedPointer<X> & ptr2 )

Returns true if the pointer ptr1 is the same pointer as that referenced by ptr2.

If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  4. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 12
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
Page suivante

Le Qt Developer Network au hasard

Logo

Compiler l'add-in Qt de Visual Studio

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web