【发布时间】:2011-12-11 15:06:53
【问题描述】:
ANSI C 不允许函数重载(我不确定 C99)。
例如:
char max(char x, char y);
short max(short x, short y);
int max(int x, int y);
float max(float x, float y);
不是有效的 ANSI C 源代码。
ANSI C 中的函数重载问题应该使用哪种技术(或想法)?
注意:
答案是重命名函数,但是应该使用哪种模式进行重命名,函数名称保持'好函数名称'?
例如:
char max1(char x, char y);
short max2(short x, short y);
int max3(int x, int y);
float max4(float x, float y);
对于max 函数名称,这不是一个好的命名。
【问题讨论】:
-
也许定义一个
max宏? ;-) -
@DidierTrosset: max 只是一个例子,想象一个非常复杂的函数。
-
如果函数重载对你很重要,那么也许你应该考虑使用 C++ 而不是 C ?
-
正如 nc3b 提到的,有一个帖子。我会根据它的答案提出这个解决方案:stackoverflow.com/questions/479207/function-overloading-in-c/…,因为它是可移植的。
标签: c naming overloading