C++ 中的运算符重载

发布于 2024-08-04 18:36:20 字数 78 浏览 4 评论 0原文

除了“新建”、“删除”、“<<” & '>>'运算符,还有哪些其他运算符可以在类上下文之外的 C++ 中重载?

Besides 'new', 'delete', '<<' & '>>' operators, what other operators can be overloaded in C++ outside of a class context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

墨离汐 2024-08-11 18:36:20

以下运算符(由空格分隔)可以作为非成员函数重载:

new delete new[] delete[] + - * / % ˆ & | ˜ ! < > += -= *= /= %= ˆ=  
&= |= << >> >>= <<= == != <= >= && || ++ -- , ->* 

以下运算符必须是非静态成员函数:

-> () [] = 

以下运算符不能重载:

. .* :: ?: # ##

转换运算符也必须是成员函数。

仅仅因为它有一个“=”并不意味着它不能作为非成员运算符重载。以下是格式良好的:

struct A { };
A operator +=(A,A) { return A(); }
A a = A()+=A();

并且前缀和后缀递增和递减运算符确实可以定义为非成员:

13.5.7 名为operator++ 的用户定义函数实现了前缀和后缀++ 运算符。如果该函数是不带参数的成员函数,或者是具有一个类或枚举类型参数的非成员函数,则它为该类型的对象定义前缀增量运算符 ++。如果该函数是具有一个参数的成员函数(应为 int 类型)或具有两个参数的非成员函数(其中第二个参数应为 int 类型),则它为以下对象定义后缀增量运算符 ++那种类型。当由于使用 ++ 运算符而调用后缀增量时,int
参数的值为零。
前缀和后缀递减运算符 -- 处理方式类似

标准中的第 13.5 条涵盖了这一点。

希望这有帮助。

The following operators (delimitted by space) can be overloaded as non-member functions:

new delete new[] delete[] + - * / % ˆ & | ˜ ! < > += -= *= /= %= ˆ=  
&= |= << >> >>= <<= == != <= >= && || ++ -- , ->* 

The following have to be non-static member functions:

-> () [] = 

The following can not be overloaded:

. .* :: ?: # ##

conversion operators also have to be member functions.

And just because it has a '=' in it does not mean it cannot be overloaded as a non-member operator. The following is well-formed:

struct A { };
A operator +=(A,A) { return A(); }
A a = A()+=A();

And the prefix and postfix increment and decrement operators can indeed be defined as non-members:

13.5.7 The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int
argument will have value zero.
The prefix and postfix decrement operators -- are handled analogously

Clause 13.5 in the Standard covers this.

Hope this helps.

陌若浮生 2024-08-11 18:36:20

可重载的运算符(逗号用作分隔符):

+、-、*、/、%、^、&、|、~、!、=、<、>、+=、-=、 *=、/=、%=、^=、&=、|=、>>=、<<=、
!=、<=、>=、&&、||、++、--、->*、(即逗号运算符)、->、[]、()、new[] 、delete[]

不能重载的运算符:
., .*, ::, ?:

重载函数必须声明为类方法的运算符:
()、[]、->、任何赋值运算符
(正如评论者指出的)

Operators that can be overloaded (comma used as delimiter):

+, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=,
!=, <=, >=, &&, ||, ++, --, ->* , (i.e., comma operator), ->, [], (), new[], delete[]

Operators that can not be overloaded:
., .*, ::, ?:

Operators where overloading function must be declared as a class method:
(), [], ->, any assignment operator
(as the commenters noted)

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