c_-_c_pointers:dynamic_casting
C - C++ Pointers - Dynamic casting
#include <iostream> using namespace std; class base1 { public: virtual void cast(){cout << " In class base1\n";} }; class base2 { public: virtual void cast() {cout << " In class base2\n";} }; class derived: public base1, public base2 { public: void cast(){cout << "In derived class \n";} }; int main() { base1* b1 = new base1; base2* b2 = new base2; derived* d = new derived; b1-> cast(); b2 -> cast(); d -> cast(); b2= dynamic_cast< base2*>(b1); if (b2) b2 -> cast(); else cout << "no cast\n"; b2 = dynamic_cast< base2*>(d); //succeeds if (b2) b2 -> cast(); else cout << "no cast\n"; return 0; }
c_-_c_pointers/dynamic_casting.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1