MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

base_code/rtti.cpp:21–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19class D: public B { };
20
21int 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected