#include <iostream>
using namespace std;
class base
{
public:
virtual bool operator ==( const base& other ) =0;
};
class derived : public base
{
public:
bool operator ==( const base& other ) { return false; }
bool operator ==( const derived& other ) { return true; }
};
int main()
{
base* a = new derived;
base* b = a;
std::cout << ( *a == *b ) << std::endl;
cout<< (dynamic_cast< base*>(b) == dynamic_cast< base*>(a));
}
std::cout << ( *a == *b ) << std::endl;
会调用Derived::operator==( const base& other )
,但为什么cout << (dynamic_cast<base*>(b) == dynamic_cast<base*>(a));
在输出中给出 1?