fork download
  1. #include <memory>
  2. #include <iostream>
  3. #include <ostream>
  4.  
  5. class BaseClass
  6. {
  7. public:
  8. int a;
  9. virtual ~BaseClass() {};
  10.  
  11. bool operator==( const BaseClass& right ) const
  12. {
  13. return typeid(*this) == typeid(right) &&
  14. a == right.a;
  15. }
  16. };
  17.  
  18. class DerivedClass : public BaseClass
  19. {
  20. public:
  21. int x;
  22.  
  23. bool operator==( const DerivedClass& right ) const
  24. {
  25. return BaseClass::operator==(right) &&
  26. x == right.x;
  27. }
  28. };
  29.  
  30. int main()
  31. {
  32. auto obj = std::make_shared<BaseClass>();
  33. auto obj2 = std::make_shared<BaseClass>();
  34. obj->a = 100;
  35. obj2->a = 100;
  36. auto result = *obj == *obj2;
  37. std::cout << "1: BaseClass(100) == BaseClass(100): " << result << std::endl;
  38.  
  39. obj2->a = 1000;
  40. result = *obj == *obj2;
  41. std::cout << "2: BaseClass(100) == BaseClass(1000): " << result << std::endl;
  42.  
  43. obj2 = std::make_shared<DerivedClass>();
  44. result = *obj == *obj2;
  45. std::cout << "3: BaseClass == DerivedClass: " << result << std::endl;
  46. }
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
1: BaseClass(100) == BaseClass(100): 1
2: BaseClass(100) == BaseClass(1000): 0
3: BaseClass == DerivedClass: 0