C++ “OR” operator

后端 未结 9 1495
无人及你
无人及你 2021-01-19 03:37

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.

相关标签:
9条回答
  • 2021-01-19 04:08

    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.

    0 讨论(0)
  • 2021-01-19 04:12

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 04:12

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

    0 讨论(0)
提交回复
热议问题