This topic is now archived and is closed to further replies.


Can someone explain operator overloading in C++

Recommended Posts

augustov3    122
Hi all, I am taking a C++ course in school. I am having trouble following the teacher on operator overloading. Can someone please explain it to me in a simple way or at least lead me to an article or website. Thanks.

Share this post

Link to post
Share on other sites
Omaha    100
Overloading an operator is a way to get an operator (like +, -, +=, new, ...) to do something else than what it normally does.

It''s useful when you have a class that you made that you want to use any of the standard operators on and have your class treat it a certain way.

Say you write a class for strings and you want this code:

String s1, s2, s3;

s1="Hi ";

To make s1 contain the string "Hi ", s2 "there!\n" and s3 is the concatenation of the two. If you were to just write that code without doing any operator overloading, you''d get errors that complained of being unable to use your class with the expressions listed there.

The way you would do get it to work would be to overload both the assignment (=) and addition (+) operators for the String class. Actually doing it is a bit more complicated, but the basic idea is you''re instructing the program how to use operators in a different way depending on what types you give to it.

Share this post

Link to post
Share on other sites
augustov3    122
Thanks for the very clear and simple explanation. I got the idea of overloading operators, but NOW I need the complicated part which is actually using it. Can someone help. I want to know how to actually use them. A small program showing its use would be very helpful.


"I haven''''t failed, I''''ve found 10,000 ways that don''''t work."
--Thomas Edison--

Share this post

Link to post
Share on other sites
Oluseyi    2112

template <class T>
class Complex
Complex(T i, T j);

// assignment operators
inline Complex &operator+= (Complex &);
inline Complex &operator-= (Complex &);
inline Complex &operator*= (Complex &);
inline Complex &operator*= (T);
inline Complex &operator/= (Complex &);
inline Complex &operator/= (T);

// binary operators
friend Complex &operator+ (Complex &, Complex &);
friend Complex &operator- (Complex &, Complex &);

// unary operators
inline Complex &operator- ();

T x, y;

// incremental assignment
template <class T>
Complex<T> &Complex<T>::operator+= (Complex<T> &c)
x += c.x; // uses += operator of class T
y += c.y;
return *this;


// subtraction. note that subtraction operator is friend and not
// member, since it requires 2 Complex objects and returns a
// third
template <class T>
Complex<T> &operator- (Complex<T> &c1, Complex<T> &c2)
Complex<T> r;
r.x = c1.x - c2.x; // uses - operator of class T
r.y = c1.y - c2.y;
return r;

// negation. note that negation operator takes no parameters;
// that is only way for compiler to distinguish between different
// operators (number of arguments and/or type)
template <class T>
Complex<T> &Complex<T>::operator- ()
// do appropriate logic to negate a complex number
// (I''m tired)

The example above declares a bunch of operators for a Complex number type. It even includes 2 operator-''s - one binary (a - b) and the other unary (a + (-b)). The body of an operator does not necessarily need to make use of built in operators, but it often does. The case of the String class given by Omaha, for example, would probably have C string manipulation functions such as strcat and strcpy in the operator body (and it would therefore be unwise to inline them).

Share this post

Link to post
Share on other sites