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
The continue statement The continue statement causes the next iteration of the enclosing loop to start. When this is encountered in the loop , the rest of the statements in the

Write a programme to display the patern.. A A B A C B A B C A B A A

Explain the terms- Substitutability and Extensibility Substitutability - The objects of a properly derived class can be safely and easily substituted for an object of its

C Program for EVEN & ODD NO IN ANY MATRICES #include stdio.h> #include conio.h> void main() {           int a[100][100];           int i=0,j=0,r,c,even=0,odd=0;

#an array that accept input and gives an output

Problem 1 What is the difference between function declaration and function definition? 2 Write a recursive function to find sum of even numbers from 2 to 10. 3 List some

should i put define constant and memory constant in my flowchart? the other one, how to draw flowchart for break and continue statement?

Explain Register Variable Computers have internal registers, which are used to keep data temporarily, before any operation can be performed. Intermediate results of the calcula

Hi is there any chance to get assignment for fresher tutor

Does improper inheritance have a potential to wreck a project? Many projects meet a dead end due to bad inheritance. Hence it certainly has potential to wreck a project. Sma