Solved

Operator Overloading in C++

Posted on 2004-03-23
4
1,043 Views
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
Blessed_smmz
0
Comment
Question by:blessed_smmz
4 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
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

Amit
0
 
LVL 12

Accepted Solution

by:
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 {
public:
      foo & operator ++ () { ....some code here...; return *this; }
};

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

Notes:

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.

Alf
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now