| 19 | class D: public B { }; |
| 20 | |
| 21 | int main() |
| 22 | { |
| 23 | B *b = new D; // 向上转型 |
| 24 | B &obj = *b; |
| 25 | D *d = dynamic_cast<D*>(b); // 向下转型 |
| 26 | if(d != nullptr) |
| 27 | cout << "works"<<endl; |
| 28 | else |
| 29 | cout << "cannot cast B* to D*"; |
| 30 | |
| 31 | try { |
| 32 | D& dobj = dynamic_cast<D&>(obj); |
| 33 | cout << "works" << endl; |
| 34 | } catch (bad_cast bc) { // ERROR |
| 35 | cout << bc.what() << endl; |
| 36 | } |
| 37 | return 0; |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected