是否允许在postfix操作符++中命名参数? [英] Is it allowed to name the parameter in postfix operator ++?

查看:163
本文介绍了是否允许在postfix操作符++中命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在任何生产环境中使用这个代码,它只是为了我的理解。这个代码是否有效(例如,我可以这样定义我的后缀操作符吗?):

I am not using this code in any production environment, it is just for my understanding. Is this code valid (i.e. can I define my postfix operator like this?):

class A
{
public:
    A& operator++(int n)
    {
        std::cout<<"N is:"<<n<<"\n";
        return *this;
    }
};


int main()
{   
    A a;
    a++;
    a.operator ++(10);
}



在VS2008上,我得到输出:

On VS2008, I get the output as:


N是0

N is 0

用于第一次呼叫和


N是10

N is 10


推荐答案

这种行为是合法的,在13.5.7中有明确定义:

This behavior is legal and well defined in 13.5.7:


显式调用操作符++,如
中的表达式,如a.operator ++(2),有
没有特殊属性:
operator ++的参数为2。

Calling operator++ explicitly, as in expressions like a.operator++(2), has no special properties: The argument to operator++ is 2.

这篇关于是否允许在postfix操作符++中命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆