C 中的运算符重载
我正在尝试重载一些运算符:
/* 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);
每次尝试重载时都会出现此错误:
expected '=', ',', ';', 'asm' or '__attribute__' before '+' token
我找不到有关运算符重载的任何好的文档。谷歌搜索结果是使用类的 C++ 教程。 C 语言中没有类。谁能帮助我吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C 不支持运算符重载(超出了语言内置的范围)。
C does not support operator overloading (beyond what it built into the language).
你需要一个时间机器来带你回到1985年,这样你就可以使用该程序CFront。看来“C”用于支持运算符重载;到足够精致的程度还是可以的。请参阅C++ 对象模型内部 作者:斯坦利·B·李普曼。天哪,C++ 就是 C!这样的事情仍然存在。
这个答案证实了其他答案。 “C”本身并不直接支持重载。然而,重要的一点是程序员可以编写能够理解代码的代码。您需要一个转换源代码的工具来实现这一点。在这种情况下,这样的工具已经存在。
一篇论文,C++ 元编译,2001 Edward D. Willink 有一些有趣的设计功能示例,其中扩展语言非常有用。 *nix shell 脚本和 make 规则的组合通常允许这种转换。其他示例有 Qt MOC、Lex 和 Yacc 工具、halide 等。 “C”本身不能直接适应这一点,如果您构建主机工具,它就可以。
在这个特定的例子中,重载可能没有意义。然而,对于需要任意精度数学的程序来说,它可能很有意义。
You need a time machine to 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 Model by Stanley B. Lippman. OMG, C++ was C! Such a thing still exists.
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.
A paper, Meta-Compilation for C++, 2001 by Edward D. Willink has interesting examples of design functionality, where extending a language is useful. The combination of *nix shell script and
make
rules often allow such transformation. Other examples are Qt MOC, the tools Lex and Yacc, halide etc. So while 'C' itself doesn't accommodate this directly, it does if you build host tools.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.
C 中没有运算符重载。
There is no operator overloading in C.
您不能在 C 中重载这些运算符。
You cannot overload these operators in C.
C 根本不支持运算符重载。
您只能将操作实现为函数:
您也可以切换到 C++,但仅仅为了重载而这样做可能有点过分了。
C does not support operator overloading at all.
You can only implement operations as functions:
You could also switch to C++, but it may be overkill to do it just for the overloading.
运算符重载在 C 中不可用。相反,您必须使用函数来“伪重载”运算符:
Operator overloading is not available in C. Instead, you will have to use a function to "pseudo-overload" the operators:
如果您想要相当的简洁性,那么使用宏是最好的选择:
...遗憾的是宏不可能使用方括号!
If you want comparable concision, the use of macros is the best available alternative:
...it's such a pity that the use of square brackets isn't possible for macros!