Moayyad Yaghi
Moayyad Yaghi

Reputation: 3722

c++ operator overloading

i'm not sure if what im talking about is an operator overloading question. is it possible to overload keywords in C++??
for example : i need to write loopOver(i=0; ;i++) instead of for(i=0;;i++) ?? is that possible in C++

and i need to have something like 2 addTo 2 instead of 2 + 2

please help thanks in advance

Upvotes: 0

Views: 229

Answers (2)

matekm
matekm

Reputation: 6030

You can use #define directive

#define loopOver for
#define addTo +

But this is just bad!

And no - this is no operator overloading question. Here You have some informations: http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading

Upvotes: 2

Jason Williams
Jason Williams

Reputation: 57892

You can't do that with operator overloading (you can't change the names of the operators, only how they work).

However, evil as it is, if you don't want to change the way they work (just the names), you would be able to achieve things like this using macros:

#define loopOver for
#define addTo +

(Use macros with extreme care though - if used incorrectly they can cause hideous problems)

Upvotes: 8

Related Questions