13.2.3 Derived-Class Member-Function Calls via base-Class pointers 解决办法: downcasting o If the address of a derived-class object has been assigned to a pointer of one of its direct or indirect base classes, it is acceptable to cast that base-class pointer back to a pointer of the derived-class type. 0 2018, SEU. All rights reserved. 16
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 16 13.2.3 Derived-Class Member-Function Calls via Base-Class Pointers 解决办法: downcasting If the address of a derived-class object has been assigned to a pointer of one of its direct or indirect base classes, it is acceptable to cast that base-class pointer back to a pointer of the derived-class type
Example o class B继承 class a,即 Bis ga o b big o big small 错 A small big 正确 °B*pBig=&sma:误 o A&refSmall big:-+B &refBig=(B &)refSmall .A*pSmall =&big: -B*pBig=(B*)pSmall B*pBig=( B*)(&small): X class a class b X 基类部分 72 派生部分 0 2018, SEU. All rights reserved. 17
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 17 Example B big; A small = big; A &refSmall = big; A *pSmall = &big; big = small; B *pBig = &small; x y class A x y class B z 基类部分 派生部分 class B 继承 class A, 即 B is a A 错 误 正 确 B *pBig = (B *) pSmall; B &refBig = (B &) refSmall; B *pBig = (B *) (&small);×
13.2.4 Virtual functions With virtual functions, the type of the object being pointed to, not the type of the handle, determines which version of a virtual function to invoke °虚函数:调用哪个(基类/派生类)虚函数,由对象类 型而不是句柄类型决定 0 2018, SEU. All rights reserved. 18
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 18 13.2.4 Virtual Functions With virtual functions, the type of the object being pointed to, not the type of the handle, determines which version of a virtual function to invoke. 虚函数: 调用哪个(基类/派生类)虚函数,由对象类 型而不是句柄类型决定