Question about #define

Hello,

if I have a function like that

1
2
3
4
5
6
7
8
int func(int n)
{
if (n==0)
{
return 1;
}
return n*(func(n-1)
}


(a simple factorial function)

and I want to define a new name to this function(with #define opertator)
but I dont want to re-write the function itself..

how do I do it?

Thanks in advance.
Last edited on
do you mean, just rename the function? you can rename it on the spot there and recompile.

or do you mean keep the original name and just make an alias for it?

#define newname(a) func(a)

At least, I think the above is accurate from what I read at
http://www.cplusplus.com/doc/tutorial/preprocessor.html
Hello

I wrote a printf function .

and than I wrote this

#define PRINT(const char* fmt, ...) myprtinf(const char* fmt, ...)

and there is an error and it says that Macro parameters must be comma-seperate..

do know what to do?
Macros aren't functions. They merely perform a text replace on the code.
Your example would be correctly written as:
1
2
3
#define PRINT(x,y) myprtinf((x),(y))
//The extra parenthesis are preferable. Notice how none of the parameters are
//typed. 

Suppose somewhere in your code you write PRINT("text",etc). After preprocessing, the code will look like this: myprtinf(("text"),(etc))
Macros don't work with the ... syntax (I can't what that is called, right now).
Humm
okay

I did like that
#define PRINT(fmt , ...) myprtinf((fmt), (...))


and in the main I wrote

PRINT("aaa %s %d aa" , "bb" , 10);


and there were two errors :
error: expected primary-expression before '...' token

and : error: expected `)' before '...' token


what I did wrong?
Last edited on
what I did wrong?

See the post above - macros don't work with ellipsis.

On that matter, don't use the ellipsis at all if you can help it (you most likely always can) and _DON'T_USE_MACROS_. They are _EVIL_. May everyone's soul who wrote a macro in a piece of code I have to work with burn in hell for eternity. Supose something simple as a the substitution of builtin bool with these defines:
1
2
3
#define bool int
#define true 1
#define false 0 

Have fun debugging if you have a variable named false anywhere (which is entirely legal and possible and won't result in any compiler error). And have fun overloading functions with bool and int parameters:
1
2
3
4
5
6
7
8
void f(int)
{
//...
}
void f(bool) // ERROR: redefinition
{
//...
}

Plus, macros don't obey namespaces, which is a pain-in-the-ass at best.

Corollary: for output, use operator<< and you don't need the '...'. If you feel you really need a function with two names, use a wrapper (I don't see the point, though, as anyone reading the code has now to remember two names for the same function).
Topic archived. No new replies allowed.