Is there a difference between i==0 and 0==i?

后端 未结 8 1386
我在风中等你
我在风中等你 2020-11-27 22:51

First code:

  if(i==0) {// do instructions here}

Second code:

  if(0==i) { // do instructions here }

What

8条回答
  •  旧巷少年郎
    2020-11-27 23:31

    For C++, it's possible, though unlikely, that there could be a difference. It depends upon what i's type is. e.g.

    struct Foo
    {
        int x;
    };
    
    bool operator==(Foo lhs, int rhs)
    {
        return lhs.x == rhs;
    }
    
    bool operator==(int lhs, Foo rhs)
    {
        std::cout << "Hi!";
        return true;
    }
    

    Someone who writes code like that should of course be shot.

提交回复
热议问题