diff --git a/c++/dcast.cpp b/c++/dcast.cpp new file mode 100644 --- /dev/null +++ b/c++/dcast.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +class A { + public: + void f () { + cout << "Ich bin f() in der Klasse A\n"; + } +}; + +class B:public A { + public: + void f () { + cout << "Ich bin f() in der Klasse B\n"; + } +}; + +class C { + public: + void f () { + cout << "Ich bin auch ein f()\n"; + } +}; + +int main () +{ + A *ap = new A (); + B *bp = new B (); + + // instanceof + C *cp = dynamic_cast < A * >(bp); + if (cp != NULL) + cp->f (); + + return 0; +}