#include <memory>
#include <iostream>
#include <ostream>
class BaseClass
{
public:
int a;
virtual ~BaseClass() {};
bool operator==( const BaseClass& right ) const
{
return typeid(*this) == typeid(right) &&
a == right.a;
}
};
class DerivedClass : public BaseClass
{
public:
int x;
bool operator==( const DerivedClass& right ) const
{
return BaseClass::operator==(right) &&
x == right.x;
}
};
int main()
{
auto obj = std::make_shared<BaseClass>();
auto obj2 = std::make_shared<BaseClass>();
obj->a = 100;
obj2->a = 100;
auto result = *obj == *obj2;
std::cout << "1: BaseClass(100) == BaseClass(100): " << result << std::endl;
obj2->a = 1000;
result = *obj == *obj2;
std::cout << "2: BaseClass(100) == BaseClass(1000): " << result << std::endl;
obj2 = std::make_shared<DerivedClass>();
result = *obj == *obj2;
std::cout << "3: BaseClass == DerivedClass: " << result << std::endl;
}
I2luY2x1ZGUgPG1lbW9yeT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8b3N0cmVhbT4KCmNsYXNzIEJhc2VDbGFzcwp7CnB1YmxpYzoKCWludCBhOwoJdmlydHVhbCB+QmFzZUNsYXNzKCkge307CgoJYm9vbCBvcGVyYXRvcj09KCBjb25zdCBCYXNlQ2xhc3MmIHJpZ2h0ICkgY29uc3QKCXsKCQlyZXR1cm4gdHlwZWlkKCp0aGlzKSA9PSB0eXBlaWQocmlnaHQpICYmIAoJCQkgICBhID09IHJpZ2h0LmE7Cgl9Cn07CgpjbGFzcyBEZXJpdmVkQ2xhc3MgOiBwdWJsaWMgQmFzZUNsYXNzCnsKcHVibGljOgoJaW50IHg7CgoJYm9vbCBvcGVyYXRvcj09KCBjb25zdCBEZXJpdmVkQ2xhc3MmIHJpZ2h0ICkgY29uc3QKCXsKCQlyZXR1cm4gQmFzZUNsYXNzOjpvcGVyYXRvcj09KHJpZ2h0KSAmJiAKCQkJICAgeCA9PSByaWdodC54OwoJfQp9OwoKaW50IG1haW4oKQp7CglhdXRvIG9iaiA9IHN0ZDo6bWFrZV9zaGFyZWQ8QmFzZUNs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