Created
March 2, 2013 08:43
-
-
Save mokomull/5070190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
struct Base { | |
enum Type { | |
BASE, | |
CHILD, | |
}; | |
int i; | |
virtual std::string getType() = 0; | |
virtual Type getEnumType() = 0; | |
}; | |
struct Child : public Base { | |
int j, k; | |
std::string getType(); | |
Type getEnumType(); | |
}; | |
std::string Child::getType() { | |
return std::string("Child"); | |
} | |
Base::Type Child::getEnumType() { | |
return CHILD; | |
} | |
Child* cp; | |
void dynamic_foo(Base* p) { | |
Child* q; | |
if (q = dynamic_cast<Child*>(p)) { | |
cp = q; | |
} | |
} | |
void stringy_foo(Base* p) { | |
if (p->getType() == "Child") { | |
Child* q = static_cast<Child*>(p); | |
cp = q; | |
} | |
} | |
void enummy_foo(Base* p) { | |
if (p->getEnumType() == Base::CHILD) { | |
Child* q = static_cast<Child*>(p); | |
cp = q; | |
} | |
} | |
int main() { | |
Child c; | |
for (int i = 0; i < 1e8; ++i) { | |
#ifdef DYNAMIC | |
dynamic_foo(&c); | |
#elif STRING | |
stringy_foo(&c); | |
#elif ENUM | |
enummy_foo(&c); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment