[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSmartPointer
Smart pointers are classes around a pointer, to prevent errors.
Boiled down to the essential resource management, a smart pointer (in this case a std::auto_ptr) looks like this (slightly modified from [1]):
Some examples of smart pointers are:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppSmartPointer
(C++) Smart pointer
'A smart pointer is a C++ class that mimics a regular pointer in syntax and some semantics, but it does more.', cited from Andrei Alexandrescu.Smart pointers are classes around a pointer, to prevent errors.
Boiled down to the essential resource management, a smart pointer (in this case a std::auto_ptr) looks like this (slightly modified from [1]):
//auto_ptr boiled down to resource management (note that it cannot do anything except that)
template <class T>
class auto_ptr
{
public:
auto_ptr(T * anyPointer = 0) : mPointer(anyPointer) {}
~auto_ptr() { delete mPointer; }
private:
T * mPointer;
};
Some examples of smart pointers are:
- std::auto_ptr (transfer of ownership on copy of pointer)
- boost::scoped_ptr (non-transferrable ownership of pointer)
- boost::scoped_array (non-transferrable ownership of array)
- boost::shared_ptr (shared ownership of pointer)
- boost::shared_array (shared ownership of array)
- boost::weak_ptr
- boost::intrusive_ptr
Reference
- 1) Scott Meyers More Effective C++
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
