Define auto pointer?, C/C++ Programming

A: The simplest instance of a smart pointer is auto_ptr that is included in the standard C++ library. Auto Pointer only takes care of Memory leak & does nothing regarding dangling pointers issue. You can determine it in the header . Here is part of auto_ptr's implementation, to illustrate what it does:

template class auto_ptr

{

T* ptr;

public:

explicit auto_ptr(T* p = 0) : ptr(p) {}

~auto_ptr() {delete ptr;}

T& operator*() {return *ptr;} T* operator->() {return ptr;}

// ...

};

As you can illustrate, auto_ptr is a simple wrapper around a regular pointer. It forwards all significant operations to this pointer (dereferencing & indirection). Its elegance within the destructor: the destructor takes care of deleting the pointer.

For the user of auto_ptr, it means that rather then writing:

void foo()

{

MyClass* p(new MyClass);

p->DoSomething();

delete p;

}

You can write down following:

void foo()

{

auto_ptr p(new MyClass);

p->DoSomething();

}

And trust p to clean up after itself.

 

Posted Date: 3/19/2013 8:58:21 AM | Location : United States







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

Write discussion on Define auto pointer?
Your posts are moderated
Related Questions
#include #include #include using namespace std; #define MAX 5 #define INF (1 #define DEBUG if(0) #define pii pair #define pb(x) push_back(x) class

write a program that counts the number of occurrences of the string in the n-th padovan string p(n)   program 1 : package test.padovanstring; public class PadovanString {

write a program to calculate gross salary and net salary using hra da pf in c++

Selecting Array Elements Implement the following C++ code in assembly language, using the block-structured .IF and .WHILE directives. Assume that all variables are 32-bit signed in

how to avoid duplicates strings in an array

Write a program to count the prime number in array


Program of Binary tree: Btree::Btree(int O) : itemsInContainer(0) {     finishInit(O); }   Btree::~Btree(void) {     if( root != 0 )         delete roo

Aim: To implement a program to swap two numbers using reference arguments. Code: void swap(int *a, int *b) {             int temp;             temp=*a;

program to write superposition of waves using class and objects