Solved

Operator Overloading in C++

Posted on 2004-03-23
4
1,038 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

17 Experts available now in Live!

Get 1:1 Help Now