Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What is a smart pointer?

Answers were Sorted based on User's Feedback



What is a smart pointer?..

Answer / akhilesh kumar jaiswal

A smart pointer is an object that acts, looks and feels like a normal pointer but offers more functionality. In C++, smart pointers are implemented as template classes that encapsulate a pointer and override standard pointer operators. They have a number of advantages over regular pointers. They are guaranteed to be initialized as either null pointers or pointers to a heap object. Indirection through a null pointer is checked. No delete is ever necessary. Objects are automatically freed when the last pointer to them has gone away. One significant problem with these smart pointers is that unlike regular pointers, they don't respect inheritance. Smart pointers are unattractive for polymorphic code. Given below is an example for the implementation of smart pointers.
Example:
template <class X>
class smart_pointer
{
public:
smart_pointer(); // makes a null pointer
smart_pointer(const X& x) // makes pointer to copy of x

X& operator *( );
const X& operator*( ) const;
X* operator->() const;

smart_pointer(const smart_pointer <X> &);
const smart_pointer <X> & operator =(const smart_pointer<X>&);
~smart_pointer();
private:
//...
};
This class implement a smart pointer to an object of type X. The object itself is located on the heap. Here is how to use it:
smart_pointer <employee> p= employee("Harris",1333);
Like other overloaded operators, p will behave like a regular pointer,
cout<<*p;
p->raise_salary(0.5);

Is This Answer Correct ?    0 Yes 0 No

What is a smart pointer?..

Answer / nashiinformaticssolutions

A smart pointer is a C++ class that manages the lifetime of a dynamically allocated object.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

How does atoi function work?

0 Answers  


What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?

0 Answers  


What is lazy initialization in c++?

0 Answers  


Why is c++ a mid-level programming language?

0 Answers  


What do you mean by C++ access specifiers ?

1 Answers  


What is a container class? What are the types of container classes?

1 Answers  


What does new in c++ do?

0 Answers  


Are vectors passed by reference c++?

0 Answers  


What is a volatile variable in c++?

2 Answers  


What is the purpose of templates in c++?

0 Answers  


What is encapsulation in c++ with example?

0 Answers  


what are the iterator and generic algorithms.

0 Answers  


Categories