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 int main() { int T; int N; int i; scanf("%d",&T)


C Program for FIND EVEN & ODD NO.S IN THE ARRAY #include stdio.h> #include string.h> #include conio.h> void main() {           int i=0,j=0, l_e[100],l_o[100];

Rahul is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''{'' and end with '

Program is to perform all the functions that are performed in a super bazaar: class stock   {   private:     int itno;     char itname[20];     char brname[20];

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

Example for Register Storage Class - computer programming? main() { register int i; for (i=0; i { ............... ............... } } /* block exit will free the register

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Write a C++ program that reads from keyboard 3 integers, with proper input prompt, and then displays the maximum sum of any pair of numbers from these three. If the 3 numbers are 5