何使用派生类指针指向基类,即downcast向下转型?

基类指针指向派生类,我们已经很熟了。假如我们想用派生类反过来指向基类,就需要有两个要求:1)马克-to-win:基类指针开始时指向派生类,2)我们还需要清清楚楚的转型一下。

if you want to use derived class pointer
point to base class, there are two requirements:
1) base class pointer is initially the type of the derived class like Animal a2 = new Dog();
2) 马克-to-win:we still need to explicitly cast it like Dog d = (Dog)a2;

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103117238

posted @ 2021-01-27 08:39  师徒行者  阅读(119)  评论(0编辑  收藏  举报