c++/dcast.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 02 May 2012 20:51:14 +0200
changeset 165 f551b78c3eee
permissions -rw-r--r--
a bluetooth and a c++ demo committer: Markus Bröker <mbroeker@largo.homelinux.org>

#include <typeinfo>
#include <iostream>

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;
}