为什么if语句为运算符重载函数评估为false?

时间:2018-03-12 13:59:59

标签: c++ if-statement

class cid
{
    int caseNo,agentNo;
public:
    cid(){}
    cid(int caseNo)
    {
        this->caseNo = caseNo;
    }
    void operator<<(int x)
    {
        this->agentNo = x;
    }
    void operator>>(int x)
    {
        this->agentNo = 0;
        if(x == this->caseNo)
            cout << "\nhello";
        else
            cout << "\nbye";
    }
    void display(void)
    {
        cout << "\ncase num is  : " << caseNo << " agent num is : " << agentNo;
    }
};

int main()
{
    cid daya(1),praduman(7);
    daya << 10;
    praduman << 1550;
    daya.display();
    praduman.display();
    daya >> 10;
    praduman >> 1550;
    daya.display();
    praduman.display();

    cout << "\npress enter to continue...";
    cin.ignore();cin.get();
    return 0;
}

输出

case num is  : 1 agent num is : 10
case num is  : 7 agent num is : 1550
bye
bye
case num is  : 1 agent num is : 0
case num is  : 7 agent num is : 0
press enter to continue...

我试图评估caseNo和x以查看它是否被分配,如果数字相同然后使agentNo为0但是调试我试图将它与1进行比较,因为已经分配为案例编号,它评估为false plz帮助

1 个答案:

答案 0 :(得分:0)

排队

cid daya(1),praduman(7);

您设置了daya.caseNo = 1praduman.caseNo = 7。与

daya << 10;
praduman << 1550;

您设置了daya.agentNo = 10praduman.agentNo = 1550。这正是

的输出
daya.display();
praduman.display();

使用daya >> 10;设置daya.agentNo = 0并检查是否if(x == this->caseNo)x的值为10,daya.caseNo1。因此,这会产生false并输出Byepraduman >> 1550;

的相同故事