// ============================================================================ // p1c06_02 — 2.2 动态转换(dynamic_cast) // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954473:1] 2.2 动态转换(dynamic_cast) --- class Animal { public: virtual void ShowName() = 0; }; class Dog : public Animal{ virtual void ShowName(){ cout << "I am a dog!" << endl; } }; class Other { public: void PrintSomething(){ cout << "我是其他类!" << endl; } }; //普通类型转换 void test01(){ //不支持基础数据类型 int a = 10; //double a = dynamic_cast(a); } //继承关系指针 void test02(){ Animal* animal01 = NULL; Dog* dog01 = new Dog; //子类指针转换成父类指针 可以 Animal* animal02 = dynamic_cast(dog01); animal02->ShowName(); //父类指针转换成子类指针 不可以 //Dog* dog02 = dynamic_cast(animal01); } //继承关系引用 void test03(){ Dog dog_ref; Dog& dog01 = dog_ref; //子类引用转换成父类引用 可以 Animal& animal02 = dynamic_cast(dog01); animal02.ShowName(); } //无继承关系指针转换 void test04(){ Animal* animal01 = NULL; Other* other = NULL; //不可以 //Animal* animal02 = dynamic_cast(other); } // --- main --- int main() { test01(); test02(); test03(); test04(); return 0; }