Can someone explain operator overloading in C++

Started by
2 comments, last by augustov3 22 years, 6 months ago
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.
"I haven''t failed, I''ve found 10,000 ways that don''t work." --Thomas Edison--
Advertisement
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 ";
s2="there!\n";
s3=s1+s2;

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.
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.

Thanks.

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

"I haven''t failed, I''ve found 10,000 ways that don''t work." --Thomas Edison--
template &ltclass T>class Complex{public:  Complex();  Complex(T i, T j);  ~Complex();  // 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- ();private:  T x, y;};// incremental assignmenttemplate &ltclass T>Complex&ltT> &Complex&ltT>::operator+= (Complex&ltT> &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// thirdtemplate &ltclass T>Complex&ltT> &operator- (Complex&ltT> &c1, Complex&ltT> &c2){  Complex&ltT> 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 &ltclass T>Complex&ltT> &Complex&ltT>::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).

This topic is closed to new replies.

Advertisement