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
a c program to find the volume of sphere

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

In POS system, cashier computer connect to printer using serial rs232 usb or cable. We need to capture data from that connection, and parsing the data to get total amount for every

Program is to read the contents of a file: void main()    {   ifstream fin("ascii.txt");   char ch;   while(! fin.eof())     {   fin>>ch;   cout     }

Can any constructor throw an exception? How to handle error while the constructor fails?

Hello, I would like to know if you can help in C programs that work on android.

Program1: Write a program to find the factorial of a given number. The input is user given. Final output is printed out on to the screen. Program2: Write a program to gene

i want a c or c++ code for solving transportation problem using modified distribution method

There are 3 acceptable answers: "Never," "Rarely "and" When the problem domain cannot be accurately modelled any other way."

Explain the Bit Wise Operators? C language has distinction of supporting special operators recognized as bit wise operators for manipulation of data at bit level. These operato