views:

137

answers:

2

hello .. 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

+8  A: 

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)

Jason Williams
Beat me to it. Arg! :)
mkgrunder
I upvoted this because it's the correct answer, but only after overcoming my reflexive urge to downvote any post which contains code that re-#defines the `for` keyword.
Charles Salvia
I agree. This is the answer to the OP, not the answer to the question "should I actually do this?", to which the answer is an emphatic "no!".
Jason Williams
Best to do this in a public header, to increase the fun for others using your library :-) :-) :-)
Allbite
+2  A: 

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%5FProgramming/Operators/Operator%5FOverloading

matekm