简体   繁体   English

除了++和-外,是否可能重载postfix一元运算符

[英]Is it possible to overload postfix unary operator besides the ++ and --

In the c++ standard said 在c ++标准中说

A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters or a non-member function with one parameter. 前缀一元运算符应由无参数的非静态成员函数(9.3)或具有一个参数的非成员函数实现。

But there is no some info about postfix operator overloading. 但是没有有关后缀运算符重载的信息。

About postfix operators (13.5.7) 关于后缀运算符(13.5.7)

The user-defined function called operator++ implements the prefix and postfix ++ operator. 用户定义的函数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. 如果函数是具有一个参数的成员函数(应为int类型)或具有两个参数的非成员函数(其第二个应为int类型),则它将为对象的后缀增量运算符++定义这种类型。 When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero. 当由于使用++运算符而调用后缀增量时,int参数的值为零。

operator-- and operator++ are unusual because they exist in both prefix and postfix forms, so something needs to be done to differentiate between the two. operator--operator++之所以与众不同,是因为它们以前缀和后缀形式存在,因此需要做一些区分两者的事情。 As you've already noted, they decided to differentiate based on having the postfix form receive an extra (unused) parameter. 正如您已经提到的,他们决定根据让后缀形式收到额外的(未使用)参数来进行区分。

You can overload other postfix operators (eg, operator[] , operator() and operator-> ), but you don't have to do anything special to overload them because they only exist in postfix form. 您可以重载其他postfix运算符(例如, operator[]operator()operator-> ),但是您不必执行任何特殊操作即可使它们重载,因为它们仅以postfix形式存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM