How would i go about using the preprocessor directives to supply an alternate name to an existing function. I'm sort of on an anti hungarian mission. Thanks.
Printable View
How would i go about using the preprocessor directives to supply an alternate name to an existing function. I'm sort of on an anti hungarian mission. Thanks.
#define BLAH(x,y) ( foo(x,y) )
int foo(int a , int b);
int main(){
int q = BLAH( 4 , 4 );
}
int foo..........................
I think that sure work, but I'm not positive
This would require memory space but an alternative would be to use a pointer.
Example
Code:int foo(int, int);
int (*bar)(int, int) = foo;
That's true but I think that defining it as a function is better because otherwise the preprocessor will change every occurance of the word even if it isn't being used as a functionQuote:
Originally posted by Salem
There's no need to include all the parameters
#define nicename pscxacSomeDumbHungarianName
like if you did
#define mult fooFunction
int fooFunction(int x, int y) { return x * y; }
int main() {
int multResult = mult(5,5);
}
the word mult in multResult will also be changed to fooFunction
so the int will be called fooFunctionResult
while in most cases the program will function the same either way, I think there will be some rare cases where there will be problems.
I tested and found out that my previous post was partially wrong, my compiler ( and I guess other ) will only rename if the word appears alone
so
#define mult foo
will change
int mult = mult(3,4);
to
int foo = foo(3,4);
but
int multResult = mult(3,4);
will only change to
int multResult = foo(3,4);
but I would still recommend using
#define mult(x,y) foo(x,y)
to avoid any problems ( and hey it's only a few extra chars anyway ).
I'd say this is the best solution:
Constant function pointers, no extra memory used.
Code:int (* const sys)(const char *) = std::system;
main()
{
sys("dir");
}
great, thanks guys, i'll probably go with the constant functions pointers.
using #define doesn't change the amount of memory used.Quote:
Originally posted by Sang-drax
I'd say this is the best solution:
Constant function pointers, no extra memory used.