   Noob question :   overloading operator-
  Hi guys,So, I recently started using C++ (came from Java and C)and I'm still getting used to a few things.I know how the -> operator is usually used, but I was wondering in what sort of situation you would want to overload the -> operator?Is this what operator+  (for example) technically works like?  :Number a(2), b(5);Number result = a + b;// result = a.operator+( b )so...MyClass* class = new MyClass();class->doStuff();// class.operator->()Is that right?By the way, sorry for the noob-ish question, but I had no luck with Google :(   
  Not often.When you want to pretend to be a pointer to something, you can overload that operator. The best known of such "pointers" are std::auto_ptr and boost's shared_ptr and weak_ptr. See the Boost Smart Pointer page.   ;  Overloading operator-> will not work when applied to pointers.MyClass myClass;myClass -> doStuff(); //myClass.operator->().doStuff()This is useful when working smart pointers. They're basically a wrapper around a pointer, but they can be used as if they were a raw pointer:template <class T>class smart_ptr{    T *ptr;    smart_ptr( T *ptr )   : ptr( ptr )    {}    ~smart_ptr()    {   delete ptr;    }    T *operator -> ()    {   return ptr;    }}smart_ptr<MyClass> myClass( new MyClass );myClass->doStuff();However you should not invent a smart_ptr class on your own (unless you don't intent to use it and do it for learning purposes), but use std::auto_ptr or boost::shared_ptr instead.You're right about overloading operator+ (and '-', '/' or '*' for that matter).   ;Ok, that makes a lot more sense now.It seems to go along with the RAII thing I just read about too.So it's used in auto_ptr, and that class basically makes sure the pointer it holds gets deleted when the auto_ptr goes out of scope?Thank you for the help :)   ;  In most caes if you overload -> you will also overload the * defrence operator so that the following both have the same effect.MyClass myObject;myObject->doSomething();(*myObject).doSomething();STL iterators also override * and -> so that they can behave more like pointer iterators.MyClass *array = new MyClass[20];MyClass *end = array + 20;for(MyClass *it=array; it!=end; ++it)    it->doSomething();std::vector<MyClass> vector(20);for(std::vector<MyClass>::iterator it=vector.begin();it!=vector.end();++it)    it->doSomething();