Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A
  6. {
  7. public:
  8. A() : a(0) {}
  9. A(int a) : a(a) {}
  10. bool operator == (const A &rhs) const { cout << "A==" << endl; return this->a == rhs.a; }
  11. private:
  12. int a;
  13. };
  14.  
  15. class B
  16. {
  17. friend bool operator == (const B &lhs, const B &rhs);
  18. public:
  19. B() : a(0) {}
  20. B(int a) : a(a) {}
  21. private:
  22. int a;
  23. };
  24. bool operator == (const B &lhs, const B &rhs) { cout << "B==" << endl; return lhs.a == rhs.a; }
  25.  
  26. int main()
  27. {
  28. A a1; A a2(1);
  29. B b1; B b2(0);
  30. cout << (a1 == a2) << endl; // OK
  31. cout << (b1 == b2) << endl; // OK также
  32. cout << (b1 == 1) << endl; // OK, 1 --> B(1)
  33. cout << (1 == b1) << endl; // OK, 1 --> B(1) тоже
  34. cout << (a1 == 0) << endl; // OK, 0 --> A(0)
  35. // cout << (1 == a2) << endl; --> error: no match for operator==(int, A)
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement