// Illustration of dynamic_cast as a predicate. #include #include using namespace std; class A { public: virtual void f(){ cout << "Here is f in A" << endl; } }; class B: public A { public: void f() override{ cout << "Here is f in B" << endl; } }; // dynamic_castsx in C++ is similar to x as T in C# class C: public A { public: void f() override{ cout << "Here is f in C" << endl; } }; int main(){ A* a = new B{}; // a can point to a B-object a->f(); // Here is f in B if (B *b = dynamic_cast(a)) // if(a is of type B*) ... b->f(); // yes: Here is f in B else cout << "Cannot" << endl; if (C *c = dynamic_cast(a)) // if(a is of type C*))... c->f(); else cout << "Cannot" << endl; // no: Cannot }