Described smart pointer?, C/C++ Programming

A: A smart pointer is a C++ class which mimics a regular pointer in syntax and some semantics, however it does more. Since smart pointers to distinct types of objects tend to have a lot of code in common, approximately all good-quality smart pointers in existence are templated trhough the pointee type, as you can notice in the following code:


class SmartPtr



explicit SmartPtr(T* pointee) : pointee_(pointee); SmartPtr& operator=(const SmartPtr& other);


T& operator*() const



return *pointee_;


T* operator->() const



return pointee_;



T* pointee_;



SmartPtr aggregates a particular pointer to T in its member variable pointee_. Most smart pointers do this. In some of the cases, a smart pointer may aggregate some handles to data and compute the pointer on the fly.

The two operators give SmartPtr pointer-like semantics and syntax. i.e., you can write

class Widget



void Fun();


SmartPtr sp(new Widget);

sp->Fun(); (*sp).Fun();

Sideways from the definition of sp, nothing reveals it since not being a pointer. It is the mantra of smart pointers: You can replace pointer definitions along with smart pointer definitions without incurring major changes to your application's code. Thus you get extra goodies along with ease. Minimizing code changes is extremely appealing and essential for getting large applications to employ smart pointers. However, smart pointers are not a free lunch.

Posted Date: 3/19/2013 9:01:12 AM | Location : United States

Related Discussions:- Described smart pointer?, Assignment Help, Ask Question on Described smart pointer?, Get Answer, Expert's Help, Described smart pointer? Discussions

Write discussion on Described smart pointer?
Your posts are moderated
Related Questions
Private derivation If no specific derivation is listed, then a private derivation is supposed. If a new class is derived privately from its parent class, then: The priva

# include stdio.h> # include string.h> # include conio.h> void main() {           int i=0,j=0,b=0,count=0;           int a[100];           for(i=0;i

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.

Incomplete types refer to pointers in which there is no availability of the execution of the referenced location or it points to some location whose value is not available for modi

C program to string compression: Write a program to define a sting and all operations on string. void main()                                 {

Write algorithms to implement the insertion and deletion operations.

Describe what is actual arguments? - When certain functions are created and used to perform an action on some provided values, some values are required to be passed to them. Th

enter height of the shape: 1 3 5 3 1 3 5 7 5 3 5 7 9 7 5 3 5 7 5 3 1 3 5 3 1

1. When developing this project in a Win32 Console Applications that includes the precompiled headers, enter in the Name: box, PRJ2[Your Full Last Name][Your First Initial] with no

derivations 1. Regardless of the type of derivation, private members are inherited by the derived class, but cannot be accessed by the new member function of the derived class,