Operator Overloading in C++

Posted on 2004-03-23
Last Modified: 2008-03-17
Its Really Urgent : Can u please write for me a simple easy program how to overload "+" and "++" operator in C++. I will be really grateful to u,Thanx
Question by:blessed_smmz
LVL 10

Expert Comment

ID: 10656032
Seems to be a homework
Sorry, we do not do homework assignments
However, we can surely help you do the same
So, post the problems u  are facing and we would help u

LVL 12

Accepted Solution

Salte earned 125 total points
ID: 10656408
well, I can give you the syntax it is usually public enough. Consult any C++ book for more info on this.

class foo {
      foo & operator ++ () { ....some code here...; return *this; }

inline const foo operator + (const foo & a, const foo & b)
{ ...some code here...; }


The operator + does not have to be inline. Especially if it is large you should rather just have the prototype in the headerfil and put the body in a .cxx or .cpp file as you do for other functions.

The operator ++ as written is also inline. Again, if the code is large you should put the body in a .cxx or .cpp file and just have the prototype in the headerfile.

The operator ++ as written above is a prefix operator. The syntax for declaring a postfix operator is this:

foo operator ++ (int)
    ......body here.....

Note that the return type in this case is a foo and not a foo reference. This is because the postfix return the old value and so have to create a new object to return. Prefix ++ return the present value and so can simply return a reference to the object it already has. This is one of the reaosns why you should normally always prefer prefix operations before postfix operations unless you really need that postfix functionality. Specifically code like this is an abomination:

for (foo k = initial_value; k < end_value; k++) ....body here....;

In this case you really just want to increase k. It is not important that it is postfix or prefix and so therefore prefix should be used. True, some compilers can in some cases figure out that the prefix one is good enough in this situation and so will pretend you did write prefix ++ but you shouldn't rely on such optimizations.

People who write loops like that are clueless period.

Also, note the int argument to the postfix ++ operator. This argument is not used it is purely a syntactic convention to tell the compiler that "this is the postfix version". Specifically it is a bad idea to declare a name for that argument and reference it in the code.

Also, note that the + operator is declared outside of the class. This is usually GOOD STYLE. The reason for this is that you often want to write other overloads of this. For example you might want to allow people to add an int and a foo together:

const foo operator + (int a, const foo & b)  { ....code here...; }

and such a function cannot be declared in the class. Putting that function outside the class and other other + overloads inside the class is messy and should be avoided. Therefore you should usually always write all + operators and similar operators outside the class - it is a good habit. Note that all operators that assigns values must be declared inside the class so += etc are always declared inside the class and never outside.

Also, note that the return type is const foo and not just foo. The reason is simple: We want to disallow code like this:

(a + b) = c;

The result of a + b should not be modified or assigned to by anyone. You should assign it to some variable or use it in a function call or as a subexpression of a larger expression and so it should only be read not modified. Therefore the operator + return a const object.

Hope this is of help.


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 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