C语言 C 中的运算符重载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3417413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operator overloading in C
提问by Jerry Coffin
I am trying to overload some operators:
我试图重载一些运算符:
/* Typedef is required for operators */
typedef int Colour;
/* Operators */
Colour operator+(Colour colour1, Colour colour2);
Colour operator-(Colour colour1, Colour colour2);
Colour operator*(Colour colour1, Colour colour2);
Colour operator/(Colour colour1, Colour colour2);
I get this error for each tried overloading:
每次尝试重载时,我都会收到此错误:
expected '=', ',', ';', 'asm' or '__attribute__' before '+' token
I can't find any good documentation on operator overloading. Googling results in C++ tutorials which use classes. In C there are no classes. Can anyone help me? Thanks.
我找不到关于运算符重载的任何好的文档。谷歌搜索结果是使用类的 C++ 教程。在 C 中没有类。谁能帮我?谢谢。
回答by Jerry Coffin
C does not support operator overloading (beyond what it built into the language).
C 不支持运算符重载(超出它内置在语言中的内容)。
回答by Justin Ardini
There is no operator overloading in C.
C 中没有运算符重载。
回答by artless noise
You need a time machineto take you back to 1985, so that you may use the program CFront. It appears that 'C' use to support operator overloading; to the sophisticated enough it still can. See Inside the C++ Object Modelby Stanley B. Lippman. OMG, C++ was C! Such a thingstill exists.
您需要一台时光机带您回到 1985 年,以便您可以使用CFront程序。似乎“C”用于支持运算符重载;到足够复杂它仍然可以。见里面的C ++对象模型由斯坦利·B·利普曼。天哪,C++ 是 C!这样的事情仍然存在。
This answer confirms the others. 'C' by itself does not directly support overloading. However, the important point is a programmer can write code that understands code. You need a tool that transforms source to implement this. In this case, such tools already exist.
这个答案证实了其他答案。'C' 本身不直接支持重载。然而,重要的一点是程序员可以编写理解代码的代码。您需要一个转换源的工具来实现这一点。在这种情况下,此类工具已经存在。
A paper, Meta-Compilation for C++, 2001by Edward D. Willink has interesting examples of design functionality, where extending a language is useful. The combination of *nix shell script and makerules often allow such transformation. Other examples are QtMOC, the tools Lex and Yacc, etc. So while 'C' itself doesn't accommodate this directly, it does if you build hosttools.
Edward D. Willink于 2001 年发表的一篇论文,C++ 元编译,提供了有趣的设计功能示例,其中扩展语言很有用。*nix shell 脚本和make规则的组合通常允许这种转换。其他示例是QtMOC、工具 Lex 和 Yacc 等。因此,虽然 'C' 本身不能直接适应这一点,但如果您构建主机工具,它就可以。
In this particular example the overloading may not make sense. However, it could make a lot of sense for a program needing arbitrary precision math.
在这个特定的例子中,重载可能没有意义。但是,对于需要任意精度数学的程序来说,它可能很有意义。
回答by Pierre-Antoine LaFayette
You cannot overload these operators in C.
你不能在 C 中重载这些运算符。
回答by Roberto Bonvallet
C does not support operator overloading at all.
C 根本不支持运算符重载。
You can only implement operations as functions:
您只能将操作实现为函数:
Colour colour_add(Colour c1, Colour c2);
Colour colour_substract(Colour c1, Colour c2);
...
You could also switch to C++, but it may be overkill to do it just for the overloading.
您也可以切换到 C++,但仅仅为了重载而这样做可能有点过分。
回答by bta
Operator overloading is not available in C. Instead, you will have to use a function to "pseudo-overload" the operators:
运算符重载在 C 中不可用。相反,您必须使用一个函数来“伪重载”运算符:
Colour add_colours(Colour c1, Colour c2) {
return c1 + c2; // or whatever you need to do
}
回答by Engineer
If you want comparable concision, the use of macros is the best available alternative:
如果你想要比较简洁,使用宏是最好的选择:
void Type_getSomething(int id); //or some other complex series of instructions
#define g(id) Type_getSomething(id)
...it's such a pity that the use of square brackets isn't possible for macros!
...很遗憾,宏无法使用方括号!

