Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class A
- {
- public:
- A() : a(0) {}
- A(int a) : a(a) {}
- bool operator == (const A &rhs) const { cout << "A==" << endl; return this->a == rhs.a; }
- private:
- int a;
- };
- class B
- {
- friend bool operator == (const B &lhs, const B &rhs);
- public:
- B() : a(0) {}
- B(int a) : a(a) {}
- private:
- int a;
- };
- bool operator == (const B &lhs, const B &rhs) { cout << "B==" << endl; return lhs.a == rhs.a; }
- int main()
- {
- A a1; A a2(1);
- B b1; B b2(0);
- cout << (a1 == a2) << endl; // OK
- cout << (b1 == b2) << endl; // OK также
- cout << (b1 == 1) << endl; // OK, 1 --> B(1)
- cout << (1 == b1) << endl; // OK, 1 --> B(1) тоже
- cout << (a1 == 0) << endl; // OK, 0 --> A(0)
- // cout << (1 == a2) << endl; --> error: no match for operator==(int, A)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement