C ++ operator "OR"

Can this be done somehow?

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

so how ... if OR b is zero then ... but it does not work 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; } 
+4
source share
9 answers

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 execute ((a || b) == 0) , it means: "It is logical or a and b equal to 0. And that is not what you want here.

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

+15
source

You must specify the condition separately each time:

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

When you do

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

has a different meaning: (a || b) means "if either a or b is non-zero (i.e. true), then the result of this expression is true." Therefore, when you do (a || b) == 0, you check to see if the result of the previously explained expression is zero (or false).

+2
source

C ++ indicates that the operands || ("or") are Boolean expressions.

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

For the built-in numeric type conversion: non-zero conversion to true, zero is converted to false. If the type p1.distanceFrom(l.p1) is of the class type Foo , the compiler will call one (and only one) user-converted transformation, for example, Foo::operator bool() , to convert the value of the expression to bool.

+2
source

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; } 
+1
source

Entertainment 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); } 

Using:

 int main(int argc, char** argv) { int a = 7; int b = 9; if (or(a, b) == 7) { } return 0; } 

It performs the same comparison that you usually do, but at a time convenient for you.

+1
source

If you have a lot of this code, you can consider a way to help:

 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 <=,>,> =, etc., perhaps you should pass the operator also as a function.

In some cases, your intentions are by writing this:

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

can be expressed bitwise or:

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

and simplified to

 return ! (a | b); 

But read the bitwise operations and carefully check it. I use them rarely, and especially I have not used C ++ for some time.

Note that you flipped the value between examples 1 and 2, returning true and false in reverse order.

And bitwise less equal does not make any sense, of course. :)

+1
source

C ++ does not support this design. Use if (a == 0 || b == 0) .

0
source

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

0
source

C ++ is not so smart. You must do each comparison manually.

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

Source: https://habr.com/ru/post/1340535/


All Articles