C++ “OR” operator

这一生的挚爱 提交于 2020-01-11 09:29:12

问题


can this be done somehow?

if((a || b) == 0) return 1;
return 0;

so its like...if a OR b equals zero, then...but it is not working for me. my real code is:

bool Circle2::contains(Line2 l) {
    if((p1.distanceFrom(l.p1) || p1.distanceFrom(l.p2)) <= r) {
        return 1;
    }
    return 0;
}

回答1:


You need to write the full expression:

(a==0)||(b==0)

And in the second code:

if((p1.distanceFrom(l.p1)<= r) || (p1.distanceFrom(l.p2)<=r) )
    return 1;

If you do ((a || b) == 0) this means "Is the logical or of a and b equal to 0. And that's not what you want here.

And as a side note: the if (BooleanExpression)return true; else return false pattern can be shortened to return BooleanExpression;




回答2:


You have to specify the condition separately each time:

if (a == 0) || (b == 0))
    bla bla;

When you do

if ((a || b) == 0)
    bla bla;

it has a different meaning: (a || b) means "if either a or b is non-zero (ie. true), then the result of this expression is true". So when you do (a||b) == 0, you are checking if the result of the previously explained expression is equal to zero (or false).




回答3:


The C++ language specifies that the operands of || ("or") be boolean expressions.

If p1.distanceFrom(l.p1) is not boolean (that is, if distanceFrom returns int, or double, or some numeric class type), the compiler will attempt to convert it to boolean.

For built in numeric type, the conversion is: non-zero converts to true, zero converts to false. If the type of p1.distanceFrom(l.p1) is of class type Foo, the compiler will call one (and only one) user defined conversion, e.g., Foo::operator bool(), to convert the expression's value to bool.




回答4:


I think you really want something like this:

bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
    return 0;
}



回答5:


Fun with templates:

template <typename T>
struct or_t
{
  or_t(const T& a, const T& b) : value1(a), value2(b)
  {
  }

  bool operator==(const T& c)
  {
    return value1 == c || value2 == c;
  }

private:
  const T& value1;
  const T& value2;
};

template <typename T>
or_t<T> or(const T& a, const T& b)
{
  return or_t<T>(a, b);
}

In use:

int main(int argc, char** argv)
{
  int a = 7;
  int b = 9;

  if (or(a, b) == 7)
  {
  }

  return 0;
}

It performs the same comparison you would normally do, though, but at your convenience.




回答6:


If you have lot of that code, you may consider a helping method:

bool distanceLE (Point p1, Point p2, double threshold) {
    return (p1.distanceFrom (p2) <= threshold)
}

bool Circle2::contains (Line2 l) {
    return distanceLE (p1, l.p1, r) && distanceLE (p1, l.p2, r);
}

If you sometimes have <, sometimes <=, >, >= and so on, maybe you should pass the operator too, in form of a function.

In some cases your intentions by writing this:

if ((a || b) == 0) return 1;
return 0;

could be expressed with an bitwise-or:

if ((a | b) == 0) return 1;
return 0;

and simplified to

return  ! (a | b);

But read up on bitwise operations and test it carefully. I use them rarely and especially I didn't use C++ for some time.

Note, that you inverted the meaning between your examples 1 and 2, returning true and false in the opposite way.

And bitwise less-equal doesn't make any sense, of course. :)




回答7:


C++ doesn't support any construct like that. Use if (a == 0 || b == 0).




回答8:


Your condition should be (a == 0 || b == 0) or (p1.distanceFrom(l.p1) <= r || p1.distanceFrom(l.p2)) <= r)




回答9:


C++ isn't that smart. You have to do each comparison manually.

bool Circle2::contains(Line2 l) {
if((p1.distanceFrom(l.p1) <= r) || (p1.distanceFrom(l.p2) <= r)) return 1;
    return 0;
}


来源:https://stackoverflow.com/questions/5058664/c-or-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!