Skip to content

Instantly share code, notes, and snippets.

@m-mizutani
Created October 3, 2012 20:19
Show Gist options
  • Save m-mizutani/3829587 to your computer and use it in GitHub Desktop.
Save m-mizutani/3829587 to your computer and use it in GitHub Desktop.
Self class verification in C++
#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;
}
@m-mizutani
Copy link
Author

output

a:1,0
b:0,1

env

  • Darwin nexus 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
  • Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment