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:

template

class SmartPtr

{

public:

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

~SmartPtr();

T& operator*() const

{

...

return *pointee_;

}

T* operator->() const

{

...

return pointee_;

}

private:

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

{

public:

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
Armed with your function from above, we can do some interesting things. For instance, any pixel where the offsets are both zero is a pit (lower than all surrounding points) .

FUNCTIONS It refers to a subprogram that is meant to do a certain task. It is basically used to execute a set of operations and return information to the main or calling functio

Array types An array is a collection of objects of a one data type. The individual objects are accessed by their position in the array. This way of accessing is known as indexi

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Inheritance and Reuse Consider a car manufacturing company. When they require building a new car, they have two choices. They can start from the scratch, or they can change an

c program for padovan string   Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the

A: By keeping along with the C++ tradition of "there's more than one method to do that" (translation: "give programmers options & tradeoffs so they can choose what's best for them

I want a PROGRAM - Facebook auto like system to increase fans to different fanpage It has in a program that take Facebook mails and passwords from a excel file (.xlsx) for examp

If you have computed a number (stored as a ?oating-point value) and want to display this on a plot (i.e. as a character string) you have to convert it into a STRING. That is we wan

Write algorithms to implement the insertion and deletion operations.