Properties of Smart Pointers
With a given class A, you can associate a smart pointer class called AP which has the following properties:
An
AP object occupies the same amount of memory space as an ordinary pointer.
An
AP object can be constructed from a pointer to an
A object.
An
AP object can be implicitly converted to a pointer to an
A object.
AP has a default constructor. An object built by this constructor implicitly converts to a null pointer to
A. Thus, you do not have to worry about uninitialized smart pointers, although you do have to be careful about such problems when you use ordinary pointers.
The operator
* is defined for
AP. It returns a reference to an object of type
A.
This operator throws an exception if the pointer is null.
The operator
-> is defined for
AP, and it returns a pointer to the same object of type
A. This operator throws an exception if the pointer is null.
The function
IlsSmartPointer::getValue returns this pointer without testing whether the pointer is non-null.
These properties mean that in use, smart pointers are practically identical to ordinary pointers, but they have the advantage of more sophisticated behavior, particularly in managing the destruction of objects.
Version 6.3
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.