, , typeid dynamic_cast.
, , ( D1 D1Bis , ).
, , ...
, .
#include<iostream>
struct BB {
virtual unsigned int GetType() = 0;
bool IsType(BB *other) {
return GetType() == other->GetType();
}
protected:
static unsigned int bbType;
};
unsigned int BB::bbType = 0;
struct B: public BB {
unsigned int GetType() override {
static unsigned int bType = BB::bbType++;
return bType;
}
};
struct D0: public B {
unsigned int GetType() override {
static unsigned int d0Type = BB::bbType++;
return d0Type;
}
};
struct D1: public B {
unsigned int GetType() override {
static unsigned int d1Type = BB::bbType++;
return d1Type;
}
};
struct D1Bis: public D1 { };
int main() {
using namespace std;
B b, bb;
D0 d0, dd0;
D1 d1, dd1;
D1Bis d1Bis;
cout << "type B == type B : " << (b.IsType(&bb) ? "true " : "false") << endl;
cout << "type B == type D0 : " << (b.IsType(&dd0) ? "true " : "false") << endl;
cout << "type B == type D1 : " << (b.IsType(&dd1) ? "true " : "false") << endl;
cout << "type B == type D1BIS : " << (b.IsType(&d1Bis) ? "true " : "false") << endl;
cout << "type D0 == type B : " << (d0.IsType(&bb) ? "true " : "false") << endl;
cout << "type D0 == type D0 : " << (d0.IsType(&dd0) ? "true " : "false") << endl;
cout << "type D0 == type D1 : " << (d0.IsType(&dd1) ? "true " : "false") << endl;
cout << "type D0 == type D1BIS : " << (d0.IsType(&d1Bis) ? "true " : "false") << endl;
cout << "type D1 == type B : " << (d1.IsType(&bb) ? "true " : "false") << endl;
cout << "type D1 == type D0 : " << (d1.IsType(&dd0) ? "true " : "false") << endl;
cout << "type D1 == type D1 : " << (d1.IsType(&dd1) ? "true " : "false") << endl;
cout << "type D1 == type D1Bis : " << (d1.IsType(&d1Bis) ? "true " : "false") << endl;
}