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
i want to check the command line argument is whether an integer or a string

Multiple Inheritance Multiple inheritance , as the name suggests , is  deriving a class from more than one class . The derived class inherits all the properties of all its base c

Operation Files 2.1. Your program should open and read the file specified by the first command line argument of the program. 2.2. The first line of the file contains an integer

HOW THE C PROGRAM CODING IMPLEMENTED DY ITSELF UNDERSTANDING . ALSO HOW I CAN A BECOME A GOOD PROGRAMMER IN C WHAT I DO FOR GOOD PROGRAMMING ,TELL ME HOW C CODING DEVELOP DY ITSELF

The program reads two numbers and a character (operator). If the character is "+" then sum is displayed. If it is "-"then difference is displayed. If it is "*" then product is disp

Protected derivation In addition to doing private and public derivations, you may also do a protected derivation. In this situation :   The private members inherited

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

Static Class Members As we already know all the objects of the class have dissimilar data members but invoke the similar member functions. Though, there is an exception to this

Introduction:  This assignment requires a knowledge of variables (integers, char types), loops, conditionals, switch, functions, char arrays, string arrays, number arrays, struc

solve mesh and nodal analysis through c/c++?