Quantcast
Channel: Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?) - Stack Overflow
Viewing all articles
Browse latest Browse all 9

Answer by Edgar Rokjān for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?)

$
0
0

Can the expression evaluate to true in C++?

Yes, nothing is impossible...

struct A {    int x = 0;};bool operator==(A& a, int num) {    return ++a.x == num;}

Then:

if ((a == 1) && (a == 2) && (a == 3)) {    std::cout << "meow"<< std::endl;}

prints meow.

But I have no idea of any practical usage of such weird overloading and, hopefully, will never see such code in production.


Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>