#### Archived

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

This topic is 6095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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.

##### Share on other sites
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--

##### Share on other sites
template <class 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 <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// thirdtemplate <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).

• 11
• 20
• 12
• 10
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999854
×