what does "overloading operators" mean

Posted on 2004-10-08
Last Modified: 2010-04-01
i am looking for a short, sweet and SIMPLE explaination of what this c++ term means
i have been searching for one in text books and on the net
but its all so complicated!!!

Question by:liseperu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Assisted Solution

msjammu earned 20 total points
ID: 12258208
Operator Overloading means:

Telling C++ operators about the classes that you create,
When you overload an operator you tell this operator how it can operate upon class you created.
C++ operators does not know how to performs on the objects of the classes that your create You nedd to tell them this.

Hope this help

Expert Comment

ID: 12258230
Suppose this,

int a ,b

int c = a + b;
C++ has already told + operator how to operate upon variables of type int.

myclass a, b
myclass c = a + b

here + does not know how to perform on objects of type myclass, SO you need to tell + operator this. (The work that C++ did for int)
LVL 22

Accepted Solution

grg99 earned 40 total points
ID: 12258246
It's basically pretty simple.

"C++" defines many predefined operators, like "+".  By default, "+" is recognized by the compiler int he context of adding numbers, both integer and float.  

Now let's say you'd like to be able to add some other data types, maybe complex numbers.  C++ doesnt know about complex numbers, or how to add them, so you could define a datatype "COMPLEX" "Struct { float real,img; } and define some functions:   complex_add( COMPLEX a, b ), and so on.   But your program wouldnt look too natural, it would look like:

sum = complex_add( a,b );     // assuming you can return a struct !! good enough for this duiscussion!

wouldnt it be much clearer if you could jsut write:   result = a + b;   ?

You can if you "overload" the "+" sign operator.  Overloading means adding YET ANOTHER meaning on top of its existing meaning.

So you can tell the C compiler:  please run this function I've given you if you see the "+" sign with a COMPLEX data type on either side of it.   The complier then swallows up this definition of yours, and if it ever sees  a + b, where a and b are the right types, it calls your function, which if you've written it correctly, will add its parameters and return the right result.

This can be a wonderful thing.  It cam make programs much clearer.

It can also be a terrible thing, In My Humble Opinion, if overdone or done clumsily.

For example, it can silently turn the "+" sign into something the casual reader wont notice is now defined as doing something very un-pluslike.  Or if you accidentally pass the wrong types, it will silently call the overloaded function when you expected the original "+" sign behavior.

Also many a C compiler puts out quixotic error messages i fit gets partially confused by an overloading declaration.

Use it wisely.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Assisted Solution

stefan73 earned 20 total points
ID: 12258761
Hi liseperu,
"Overloading" in general means that there are several versions of a function or method with an identical name. The compiler chooses the appropriate one by using the parameters supplied with the call.

A simple overloading example:
void increment(int& var){

void increment(int& var, const int& by_what){

(You'd normally use only the 2nd version, with 1 as a default value for by_what)

Depending on whether you call the function with one or two parameters, the compiler either chooses the first or the second version.

Operators are just "syntactical sugar" for a function or method call. Those calls are identical:

a.operator+(b)     (method version)
operator+(a,b)     (function version)



Assisted Solution

GFinseth earned 20 total points
ID: 12259013
Overloading means the same operator or function behaves differently depending on how you use it.

C actually did some of this, since operators like + could be used on more than one type.  If you wrote z = a + b, it works properly whether the variables are chars, ints, or floats.

C++ extends this to let you overload your own operators and functions.  You could add more versions of + to add together imaginary numbers, or vectors, or whatever you want.

In C, if you wrote a function to print two values, you'd need a differently named version for each type, like print_ints(int a, int b) and print_floats(float a, float b).

In C++, you can have print(int a,int b) and a seemingly identically named print(float a, float b).

The compiler changes the name behind the scenes based on the argument types, sort of like tacking on the _int and _float for you.  Then the linker can automatically grab the right one.

For your part, you have to write both versions.  The big deal with overloading is they can have the exact same name and the right one gets used automatically.

Expert Comment

ID: 12259924
you tell the operator to __do_more_that_it_normally does__. thus, you "overload" the operator. I don't have much to add here. since othes experts have provided good enough explanation.

Assisted Solution

aravindtj earned 20 total points
ID: 12260527

simple example.

string a = "experts-";
string b = "exchange";

int c = 10;
int d = 20;

string e;
int f;

e = a + b; // + operator is overloaded for concatination
f = c + d; // + is addition - normal.

for how to, refer some good books.

Expert Comment

ID: 12317927
Overloading means that you create a new signature(parameters and optionally return value) to
function/class method. It may be used to provide user with option to use one "term" with varying/changing arguments, for example
int Add(int t,int t2);
long Add(long l,long ll);
instead of
int Add_2_integers(int t,int t2);
long Add_2_longs(long l,long ll);

Also consider this:
class foo
   int bar();
   void bar(); // error, differ only by return type
   int bar(int t); //ok
   int bar(long t); // if sizeof(long)!=sizeof(int)
   void bar(int t); // ok

overloading operators:
long operator+(long one,long two)

double operator+(double one,double two)

Author Comment

ID: 12389841
thanks for your help everyone
i have finally understand


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question