A smart pointer is designed and intended to mimic a raw pointer as much as possible, simply provide automatic memory management but otherwise be transparent to outside code. That means overriding the ->
, =
, and &
operators, implementing conversion operations, etc. This way, the rest of the code can treat the smart pointer as if it were a real pointer in almost all aspects.
Imagine someone started with this code:
BSTR userName;
SUCCEED(getUserName(&userName));
if(userName == NULL) ...
Then wanted to upgrade to a smart pointer:
CComBSTR userName;
SUCCEED(getUserName(&userName));
if(userName == NULL) ...
See how that works? Only one line changed.