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

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.

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 96
c++ syntax question 9 49
Which Linux flavors will this run on? 6 88
Installshield for Embarcadero EX 10.1 Berlin 4 42
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

777 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