Wednesday, April 11, 2007

OO concepts with C

As usual my example is more related to simple C and not Objective C.

Here, is the second tip.
How can i use function overloading in C?
Answer is ,you can provided you are absolutely sure what you are doing. This means you should be double sure of your parameters passed and avoiding any smart newbie stuff like passing an expression as and argument, which many people do to flaunt their horrible obscure C programming constructs.(No pun intended cuz i am also one of them :)).
You can impersonate function overloading using macros and inline functions in C(more specifically C99). Before you take this suggestion, remember Macros are a bad way of coding.They show how lazy you are. Thank C99 for inline functions.

Implementation :-
#define my_function(x) \
do { \
//do something sane \
// with x , with \
// one expression per line \
} while(0);

void my_function()
//kill'em all :)

#define my_function(y, x) \
do {
x = y+1;
while }(0);

Weird it seems but effective.




amit said...

its not wiered...amazing...i m really surprised to see all these..u r amazing...

psr said...

Damn i was wrong :-/
messed up somethings.
Please read this comment before following this.

The way i have posted it, it is WRONG!!!