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
how can I find the GCD of 2 given numbers using Euclidean Algorithm ?

program to find if a no . is prime or not

THIS PROGRAM IS TO ASK A DATE & ALSO ASK FOR DAYS TO BE ADDED TO PRODUCE #include #include struct date  {   int dd;   int yy;   int mm;  }; void main()  {  clrscr();  in

Question : (a) How does a structure differ from an array? (b) Describe how a structure differs from a union? (c) Declare a union called ‘clothes' which consists of the

The #define Directive The #define directive explains a macro which is a text string represented by a name. Whenever the pre-processor finds this name in the program, it is repl


Define some features of external storage class in c program? The features of an external storage class variable are as follows: Storage - memory Default initial value -

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

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

Define the following Window class : - integer  data members, width and height - a constructor  that accepts two integer  parameters  (width followed by height) and uses them to ini