Created
October 3, 2012 20:19
-
-
Save m-mizutani/3829587 to your computer and use it in GitHub Desktop.
Self class verification in C++
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 <stdlib.h> | |
#include <iostream> | |
class ChildA; | |
class ChildB; | |
class Base { | |
public: | |
bool isChildA (); | |
bool isChildB (); | |
virtual void hoge () { | |
} | |
}; | |
class ChildA : public Base{ | |
}; | |
class ChildB : public Base { | |
}; | |
bool Base::isChildA () { | |
return (NULL != dynamic_cast <ChildA *> (this)); | |
} | |
bool Base::isChildB () { | |
return (NULL != dynamic_cast <ChildB *> (this)); | |
} | |
int main () { | |
Base *a = new ChildA (); | |
Base *b = new ChildB (); | |
std::cout << "a:" << a->isChildA () << "," << a->isChildB () << std::endl; | |
std::cout << "b:" << b->isChildA () << "," << b->isChildB () << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output
env