upcasting : Assigning a pointer of a derived class type to a pointer of its base class type (将派生类类型指针赋值给基类类型指针
downcasting : Assigning a pointer of a base class type to a pointer of its derived class type. (将基类类型指针赋值给派生类类型指针)
注意这里的upcasting/downcasting的探讨范围是指针的赋值:且所谓向上向下方向是从右向坐看方向。
上转可不适用dynamic_cast而隐式转换
eg:GeometricObject *g = new Circle(1);
Circle *c = new Circle(2);
g = c; //Correct
下转必须显式执行
c = dynamic_cast(g);//不可以直接赋值,进行隐式转换,只能dynamic_cast
注意:基类与派生类的赋值互操作(对象,指针,引用)都遵从一个原则:可以截断,不可无中生有。
时间: 2024-10-15 17:22:24