Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 313 - 313 of 354

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 313

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is a small pointer?

Explanation

  • A smart pointer works like a normal pointer but offers more functionality.
  • In C ++ , smart pointers are implemented as template classes encapsulating a pointer, overriding standard pointer operators.
  • They are guaranteed to be initialized as either null pointers or pointers to a heap object.
  • They check for indirection through a null pointer.
  • Deleting them is not necessary. Smart objects are freed when last pointer to them is deallocated.
  • Smart pointers however, do not respect inheritance. Hence they cannot be used for polymorphic code.

For example,

template 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 &) ; const smart_pointer & operator = (const smart_pointer&) ; ~smart_pointer () ; private: // …} ;

  • This class implements 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 p = employee ( “Harris” , 1333) ; Like other overloaded operators, p will behave like a regular pointer, cout << ⚹ p; p ⇾ raise_salary (0.5) ;

Developed by: