Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <typeinfo>
- using namespace std;
- class A {
- public:
- virtual void fx() {
- cout << "A::fx" << endl;
- }
- };
- class B: public A {
- public:
- void fx() {
- cout << "B::fx" << endl;
- }
- };
- class C: public A {
- public:
- void fx () {
- cout << "C::fx" << endl;
- }
- };
- void f(A* p, A& r) {
- if (B* pp = dynamic_cast <B*> (p)) {
- cout << "using of pp" << endl;
- } else {
- cout << "NULL" << endl;
- }
- cout << "STILL HERE" << endl;
- B& pr = dynamic_cast <B&> (r);
- }
- void g() {
- try {
- cout << "f(new B, *new B) - correct using" << endl;
- f(new B, *new B);
- cout << "f(new C, *new C) - incorrect using" << endl;
- f(new C, *new C);
- cout << "STILL HERE" << endl;
- }
- catch(bad_cast) {
- cout << "Bad_cast" << endl;
- }
- }
- int main() {
- g();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement