#include<iostream> using namespace std; struct A{ char ch; int val; friend bool operator==(const A &ob1, const A &ob2); bool operator==(const A &rhs); }; bool operator==(const A &ob1, const A &ob2){ return (ob1.ch == ob2.ch && ob1.val == ob2.val); } bool A::operator==(const A &rhs){ return (ch == rhs.ch && val == rhs.val); } int main(){ struct A s3,s4; A s, s1; A s2; s1.ch = 1; s1.val = 2; s2.ch = 1; s2.val = 2; if (s1 == s2){ cout << "s1 is equal with s2" << endl; } else{ cout << "s1 is not equal with s2" << endl; } return 0; }
原文:https://www.cnblogs.com/wanghao-boke/p/14641453.html