From the user's point of view, a smart pointer is similar to a typedef used to mask a C/C++ Pointer:
Smart Pointers encapsulate C/C++ Pointer to protect pointer from address and/or data corruption.
Here a list of the advantages:
Dtk_SmartPtr is a template class.
So it should be used with Dtk_SmartPtr<XXX> with XXX the encapsulated class.
However, DATAKIT Library provide typedef to make SmartPtr Handling easier.
The typedef is created in this way:
where XXX is the encapsulated class.
All classes that can be handled with DtkSmartPtr have 'Create' methods. Theses methods are like constructors (new) but they return a DtkSmartPtr to the Object. NB: For almost classes. It's not possible to create static objects or dynamic objects with the new operator.
The Dtk_SmartPtr classes provide methods for user-defined conversion, to make the usual C/C++ pointer tests possible:
In some cases, we need to duplicate data to keep track of the main entity.
The Dtk_SmartPtr classes provide methods to clone objects:
Consider an API class XXX (for Example Dtk_symbol) derived from an API class YYY (for example Dtk_2dEntity).
The Dtk_SmartPtr class XXXPtr provides a static method to "promote" a Dtk_SmartPtr YYYPtr to a Dtk_SmartPtr XXXP (or vice versa).
if class XXX is not derived from class YYY. Promoting the YYYPtr to YYYPtr (or vice versa) will result in a NULL YYYPtr.
In theory, you don't have to delete a Dtk_SmartPtr because it will be automatically deleted. However, if you want to manually delete a Dtk_SmartPtr you just have to affect a NULL pointer.