si , scusa .
questo è lo smart pointer che uso:
header:
.cppcodice:// Geometric Tools, LLC // Copyright (c) 1998-2010 // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt // http://www.geometrictools.com/Licens...ICENSE_1_0.txt // // File Version: 4.10.0 (2009/11/18) #ifndef WM4SMARTPOINTER_H #define WM4SMARTPOINTER_H #include "Wm4GraphicsLIB.h" namespace Wm4 { template <class T> class Pointer { public: // construction and destruction Pointer (T* pkObject = 0); Pointer (const Pointer& rkPointer); ~Pointer (); // implicit conversions operator T* () const; T& operator* () const; T* operator-> () const; // assignment Pointer& operator= (T* pkObject); Pointer& operator= (const Pointer& rkReference); // comparisons bool operator== (T* pkObject) const; bool operator!= (T* pkObject) const; bool operator== (const Pointer& rkReference) const; bool operator!= (const Pointer& rkReference) const; protected: // the shared object T* m_pkObject; }; #include "Wm4SmartPointer.inl" } #endif
con typedef Pointer<Cnomeclassetiposmartpointer> smartpointerPtr;codice:// Geometric Tools, LLC // Copyright (c) 1998-2010 // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt // http://www.geometrictools.com/Licens...ICENSE_1_0.txt // // File Version: 4.10.0 (2009/11/18) //---------------------------------------------------------------------------- template <class T> Pointer<T>::Pointer (T* pkObject) { m_pkObject = pkObject; if (m_pkObject) { m_pkObject->IncrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::Pointer (const Pointer& rkPointer) { m_pkObject = rkPointer.m_pkObject; if (m_pkObject) { m_pkObject->IncrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::~Pointer () { if (m_pkObject) { m_pkObject->DecrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::operator T* () const { return m_pkObject; } //---------------------------------------------------------------------------- template <class T> T& Pointer<T>::operator* () const { return *m_pkObject; } //---------------------------------------------------------------------------- template <class T> T* Pointer<T>::operator-> () const { return m_pkObject; } //---------------------------------------------------------------------------- template <class T> Pointer<T>& Pointer<T>::operator= (T* pkObject) { if (m_pkObject != pkObject) { if (pkObject) { pkObject->IncrementReferences(); } if (m_pkObject) { m_pkObject->DecrementReferences(); } m_pkObject = pkObject; } return *this; } //---------------------------------------------------------------------------- template <class T> Pointer<T>& Pointer<T>::operator= (const Pointer& rkPointer) { if (m_pkObject != rkPointer.m_pkObject) { if (rkPointer.m_pkObject) { rkPointer.m_pkObject->IncrementReferences(); } if (m_pkObject) { m_pkObject->DecrementReferences(); } m_pkObject = rkPointer.m_pkObject; } return *this; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator== (T* pkObject) const { return m_pkObject == pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator!= (T* pkObject) const { return m_pkObject != pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator== (const Pointer& rkPointer) const { return m_pkObject == rkPointer.m_pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator!= (const Pointer& rkPointer) const { return m_pkObject != rkPointer.m_pkObject; } //----------------------------------------------------------------------------
creo lo smart pointer di tipo "Cnomeclassetiposmartpointer"
per smart pointers annidati volevo dire che la classe da cui creo lo smart pointer incapsula altri smart pointers come variabili membro:
inoltre ho un dubbio ,se ho degli smartpointers in un container stl e richiamo clear() si decrementa il reference counter?codice:class Cnomeclassetiposmartpointer { SmartPointerA; SmartPointerB; };
come fa a scegliere se decrementare / incrementare il reference counter?
grazie.
pardon , l'inc:
codice:// Geometric Tools, LLC // Copyright (c) 1998-2010 // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt // http://www.geometrictools.com/Licens...ICENSE_1_0.txt // // File Version: 4.10.0 (2009/11/18) //---------------------------------------------------------------------------- template <class T> Pointer<T>::Pointer (T* pkObject) { m_pkObject = pkObject; if (m_pkObject) { m_pkObject->IncrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::Pointer (const Pointer& rkPointer) { m_pkObject = rkPointer.m_pkObject; if (m_pkObject) { m_pkObject->IncrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::~Pointer () { if (m_pkObject) { m_pkObject->DecrementReferences(); } } //---------------------------------------------------------------------------- template <class T> Pointer<T>::operator T* () const { return m_pkObject; } //---------------------------------------------------------------------------- template <class T> T& Pointer<T>::operator* () const { return *m_pkObject; } //---------------------------------------------------------------------------- template <class T> T* Pointer<T>::operator-> () const { return m_pkObject; } //---------------------------------------------------------------------------- template <class T> Pointer<T>& Pointer<T>::operator= (T* pkObject) { if (m_pkObject != pkObject) { if (pkObject) { pkObject->IncrementReferences(); } if (m_pkObject) { m_pkObject->DecrementReferences(); } m_pkObject = pkObject; } return *this; } //---------------------------------------------------------------------------- template <class T> Pointer<T>& Pointer<T>::operator= (const Pointer& rkPointer) { if (m_pkObject != rkPointer.m_pkObject) { if (rkPointer.m_pkObject) { rkPointer.m_pkObject->IncrementReferences(); } if (m_pkObject) { m_pkObject->DecrementReferences(); } m_pkObject = rkPointer.m_pkObject; } return *this; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator== (T* pkObject) const { return m_pkObject == pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator!= (T* pkObject) const { return m_pkObject != pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator== (const Pointer& rkPointer) const { return m_pkObject == rkPointer.m_pkObject; } //---------------------------------------------------------------------------- template <class T> bool Pointer<T>::operator!= (const Pointer& rkPointer) const { return m_pkObject != rkPointer.m_pkObject; } //----------------------------------------------------------------------------

Rispondi quotando