Game Development Reference
In-Depth Information
6.
The dereference operator ( -> ) is one of the crucial features of any smart pointer:
inline T* operator -> () const
{
return FObject;
}
7.
Mimic a dynamic_cast behavior:
template <typename U>
inline clPtr<U> DynamicCast() const
{
return clPtr<U>( dynamic_cast<U*>( FObject ) );
}
8.
The comparison operator is also implemented:
template <typename U>
inline bool operator == ( const clPtr<U>& Ptr1 ) const
{
return FObject == Ptr1.GetInternalPtr();
}
9.
Sometimes, we need to pass a value of a smart pointer to a third-party C API.
We need to retrieve an internal pointer to do it:
inline T* GetInternalPtr() const
{
return FObject;
}
private:
T* FObject;
};
Refer to the example 4_ReferenceCounting_ptr from the topic's supplementary materials
for the full source code.
How it works...
The minimalistic example that demonstrates the usage of our smart pointer is as follows:
class SomeClass: public iObject {};
void Test()
{
clPtr<SomeClass> Ptr = new SomeClass();
}
 
Search WWH ::




Custom Search