在C中显式调用原始运算符函数

在C中显式调用原始运算符函数,第1张

概述int a, b, c; //do stuff. For e.g., cin >> b >> c; c = a + b; //works c = operator+(a,b); //fails to compile, 'operator+' not defined. 这另一方面起作用 – class Foo{ int x; public: Foo(int x):
int a,b,c; //do stuff. For e.g.,cin >> b >> c; c = a + b;          //works c = operator+(a,b); //fails to compile,'operator+' not defined.

这另一方面起作用 –

class Foo{ int x; public: Foo(int x):x(x) {}  Foo frIEnd operator+(const Foo& f,const Foo& g) {  return Foo(f.x + g.x);  }};    Foo l(5),m(10); Foo n = operator+(l,m); //compiles ok!

>甚至可以直接调用原始类型的运算符(和其他运算符)(如int)吗?
>如果是,怎么样?
>如果没有,是否有C参考词汇表明这是不可行的?

解决方法 首先,将内置运算符作为函数调用将不起作用,因为语言规范从未说过存在这样的函数.内置 *** 作符只是 *** 作符.它们背后没有实现功能,因为语言规范从未暗示它们的存在.基于函数的实现仅适用于重载运算符.

其次,在重载解析期间,内置运算符确实由它们虚构的函数类似对应物表示,但禁止“内置运算符”的“显式”函数式调用的措辞存在于13.6 / 1中

The candIDate operator functions that
represent the built-in operators
defined in clause 5 are specifIEd in
this subclause. These candIDate
functions participate in the operator
overload resolution process as
described in 13.3.1.2 @H_502_26@and are used for no other purpose.

总结

以上是内存溢出为你收集整理的在C中显式调用原始运算符函数全部内容,希望文章能够帮你解决在C中显式调用原始运算符函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1255910.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存