add Chapter01 examples
This commit is contained in:
29
Chapter01/rtti.cpp
Normal file
29
Chapter01/rtti.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <cstdio>
|
||||
|
||||
struct Base {
|
||||
virtual void print() {
|
||||
printf("Base\r\n");
|
||||
}
|
||||
};
|
||||
|
||||
struct Derived : public Base {
|
||||
void print() override {
|
||||
printf("Derived\r\n");
|
||||
}
|
||||
};
|
||||
|
||||
void printer(Base &base) {
|
||||
base.print();
|
||||
|
||||
if(Derived *derived = dynamic_cast<Derived*>(&base); derived!=nullptr) {
|
||||
printf("We found Base using RTTI!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Base base;
|
||||
Derived derived;
|
||||
printer(base);
|
||||
printer(derived);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user