为什么将数据类型int作为参数传递给operator函数? [英] Why the data type int is passed as a parameter in the operator function?

查看:160
本文介绍了为什么将数据类型int作为参数传递给operator函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么将数据类型int作为参数传递给运算符?如果需要邮递增量++,则必须通过此操作吗?

Why the data type int is passed as a parameter in the operator function? is it compulsory to pass this in case of post increment ++?

#include<iostream.h>
#include<conio.h>

class x
{
      private:
              int a;
      public:
             x(int j)
             {
                   a=j;
             }
             x operator++(int)        //postfix 
             {
                    return(a++);
             }
             void display()
             {
                  cout<<a;
             }
};

int main()
{
    x o1(10);
    cout<<"Before increment :";
    o1.display();
    o1++;
    cout<<endl;
    cout<<"After increment :";
    o1.display();
    getch();
    return 0;
}

推荐答案

简单:区分预增量(无类型)与后增量(有类型)

否则,您将不得不使用两个不同的名称,这有点违反OOP的原理.这样,它看起来就像是一个标准的重载运算符.
Simple: to differentiate Pre-increment (no type) from Post-increment (with type)

Otherwise you would have to have two different names, with is a bit against the principles of OOP. This way it just looks like a standard overloaded operator.


这篇关于为什么将数据类型int作为参数传递给operator函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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