Operators Overloading

What is Operator Overloading

Operator Overloading enables us to make the standard operators, like +, -, * etc, to work with the objects of our own data types. So what we do is, write a function which redefines a particular operator so that it performs a specific operation when it is used with the object of a class.

Operator overloading does not allow to make new operators or change the precedence of the operator. There are some operators which can not be overloaded.

Operator Description
:: The scope resolution operator
: ? The conditional operator
sizeof The sizeof operator
. The direct member selection operator
.* The de-reference pointer to class member operator

Example of Operator Overloading

C++ allows you to overload operators. This means, for example we can redefine the > operator so that, when it is used with the object of a class ShoeBox, it would return true if the first ShoeBox had the greater volume than the second ShoeBox object.

Here operator< id overloaded for ShoeBox.

Here is the code that shows how we can compare two ShoeBox objects.

And here is the code which actually implements the overloading process.

There are two things to understand here.

  1. We have given the reference parameter to th function to avoid unnecessary copying
  2. As the function does not alter any this so we can declare it as const.

Many books show the syntax for declaring overloaded operators, but few offer guidelines for keeping the amount of code you write to a minimum and for avoiding code duplication. The guidelines in this tutorial do not necessarily result in the most efficient code from an execution standpoint, but development efforts are minimized — efficiency and maintainability from a coding standpoint is emphasized. Of course once you’ve succeed in implementing overloaded operators you can then concentrate on making things efficient.

19 Comments

  1. Please understand I’m still a learner if my questions might somehow sound sheepish, but I hope you’ll be nice to like you’ve always been. Could you please send me a tutorial on the obove subject. I really see smoke on fire in this topic.

    Thank you in advance for your usual corperation

    rafubemojave at yahoo dot com

  2. Hi can anybody let me know how to implement the sizeof operator ourself. I succeded in implementing it partially.
    For eg for int a;mysizeof(a); will give 2/4 bytes depending on the OS but i am unable to create it for teh func calls like mysizeof(int)

    Any ideas….
    Thanks in Advance

    • I dont have any idea how to implement.
      For what purpose you are implementing this function?
      If a facility is provided then why not use it?

      ——– Original Message ——–
      Hi can anybody let me know how to implement the sizeof operator ourself. I succeded in implementing it partially.
      For eg for int a;mysizeof(a); will give 2/4 bytes depending on the OS but i am unable to create it for teh func calls like mysizeof(int)

      Any ideas….
      Thanks in Advance

    • ——– Original Message ——–
      Hi can anybody let me know how to implement the sizeof operator ourself. I succeded in implementing it partially.
      For eg for int a;mysizeof(a); will give 2/4 bytes depending on the OS but i am unable to create it for teh func calls like mysizeof(int)

      Any ideas….
      Thanks in Advance

    • Well ..* operators are used to deal with the classes, The ternary operator (?:) is so different, but so little used that it’s not worth the headaches to provide the overloading mechanisms.
      Overloading sizeof could create major problems in situations involving dynamic memory or binary data record I/O.

      ——– Original Message ——–
      Why it is not possible to overload opearators like sizeof ,?,::,.,*

    • 1)There is no meaning of overloading ternary operator ?:
      2)If you overload . then how will you access its members,so compiler itself block it to do.
      3)* we can overload.
      4)In many operation compiler uses sizeof() to calculate the size for example increamenting the pointer,so for these things the result of your program may be unpredictable.So for this reason compiler itself protect its overloading.

      ——– Original Message ——–
      Why it is not possible to overload opearators like sizeof ,?,::,.,*

    • Check the string.h library

      ——– Original Message ——–
      I need to know how + opeator s used for string concatenation using constructors.

  3. m thnkful 4 all the compact n precise details given here bt i wanna knw abt programs making.
    lyk everytym i make programi get error in it.most common is INCORRECT TERMINATION n m nt getting ny way out 2 solve it.

Leave a Reply