C 中的运算符重载

发布于 2024-09-12 21:17:05 字数 506 浏览 6 评论 0原文

我正在尝试重载一些运算符:

/* 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

过气美图社 2024-09-19 21:17:05

C 不支持运算符重载(超出了语言内置的范围)。

C does not support operator overloading (beyond what it built into the language).

天生の放荡 2024-09-19 21:17:05

你需要一个时间机器来带你回到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.

蹲墙角沉默 2024-09-19 21:17:05

C 中没有运算符重载。

There is no operator overloading in C.

爱本泡沫多脆弱 2024-09-19 21:17:05

您不能在 C 中重载这些运算符。

You cannot overload these operators in C.

盗心人 2024-09-19 21:17:05

C 根本不支持运算符重载。

您只能将操作实现为函数:

Colour colour_add(Colour c1, Colour c2);
Colour colour_substract(Colour c1, Colour c2);
...

您也可以切换到 C++,但仅仅为了重载而这样做可能有点过分了。

C does not support operator overloading at all.

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.

囚我心虐我身 2024-09-19 21:17:05

运算符重载在 C 中不可用。相反,您必须使用函数来“伪重载”运算符:

Colour add_colours(Colour c1, Colour c2) {
    return c1 + c2; // or whatever you need to do
}

Operator overloading is not available in C. Instead, you will have to use a function to "pseudo-overload" the operators:

Colour add_colours(Colour c1, Colour c2) {
    return c1 + c2; // or whatever you need to do
}
神爱温柔 2024-09-19 21:17:05

如果您想要相当的简洁性,那么使用宏是最好的选择:

void Type_getSomething(int id); //or some other complex series of instructions

#define g(id) Type_getSomething(id)

...遗憾的是宏不可能使用方括号!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文