113. Operator Overloading-I

Question.3

A developer compares two sensor readings:

struct SensorData { int id; float val; };

bool operator==(const SensorData& a, const SensorData& b) {
   return a.id == b.id && a.val == b.val;
}

SensorData s1{1, 25.5f}, s2{1, 25.5f};
if (s1 == s2) printf("Match");

Why is this a free function (not a member)?

Need Help? Refer to the Quick Guide below

Select Answer