Geekpedia Programming Tutorials






Smart Pointers - Part 1

Programming tutorial for understanding "Smart Pointers".

On Wednesday, May 19th 2004 at 07:04 AM
By Hamed Zaghaghi (View Profile)
***--   (Rated 2.7 with 15 votes)
Contextual Ads
More C++ Resources
Advertisement

Smart Pointers :
"In C++, an object that implements the functionality of a pointer and additionaly performs some action whenever an object is accessed through it. Smart pointers are implemented by overloading the pointer-derefrence(->) operator".

above is about smart pointers,
but
how to overload the -> operator?
how to implement a "smart pointers"?
and are "smart pointers" useful?
are the FAQ of "smart pointers".
in this part I show that how to overload the -> operator by this example:


  class sp1
  {
       public:
           int data;
           sp1* operator ->() { return this;};
  };

  int main()
  {
       sp1 t;
       cin >> t.data;
       cout << t->data;
  }

see that (->) operator work the same as (.) operator.
NOTE:
(.) oprator can't be overloaded.
GoodLuck.

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Sina Marandinan on Friday, May 21st 2004 at 09:38 PM

Nice, It is an intresting technic, tanx.

by Sina Marandinan on Friday, May 21st 2004 at 09:40 PM

Nice, It is an intresting technic, tanx.

by Shiva on Saturday, October 30th 2004 at 04:09 AM

Small and Superb tip to freshers who are new to C++ and guys who want to familiar with C++ Pointers.

by whatever on Friday, January 11th 2008 at 10:46 PM

You dont explain the part about the pointer being smart, how is that part used and how would one benefit from it in a program. kind of brief description.

still good effort


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons