Overloading is the reuse of the same symbol or function name for two or more distinct operations or functions. Whilst this may sound confusing, used carefully it helps to keep code transparent. Overloading can be used with operators and functions.
In C++, this concept is pushed much further. It has a large operator set and almost all of them can be overloaded by user functions when involving expressions with objects. So it would be possible to develop a matrix class and then define addition, multiplication and so forth for it. C++ considers [] array and () - function call as operators. So it is possible to write constructions like:-
MyObj[] and MyObj()and have C++ treat this as a call to MyObj's member functions. One place where the function operator is overloaded is in ROOT's TIter class. It not uncommon to see code similar to this:-
TIter(MyCollection) next; ... next() ...In this class the function operator just calls the Next member function so next() is the same as next.Next(), but looks less odd (possibly).
max( 3, 5 )and
max( 3. ,5. )in one case selecting the largest integer and in the other the largest real. Again C++ extends the overloading concept, allowing the user to overload functions. We have already seen a simple example of this in the OO topic Constructors & Destructors in which our Track class had 2 constructors:-
Track::Track(Float_t mass, Float_t energy); Track::Track();Constructors are frequently overloaded. C++ requires a default i.e. one to be used without any arguments, but many interesting constructors require the user to qualify the initial state. Sometimes a single constructor can serve both functions as C++ allows default arguments. We could declare our track constructor as:-
Track::Track(Float_t mass = 0., Float_t energy = 0.);and then create a track with:-
Track MyTrack(0.135);and have the compiler call the constructor function with energy = 0. As it stands we would now be in trouble if we did:-
Track MyTrack;as the compiler has a choice: either use the default constructor, or the other with both arguments set to zero.