当发送类实例时是否会调用重写,就好像它是没有重写的类型一样?
拥有扩展类 A 并重写其函数的类 B,我们可以确定当发送 (B*) 的实例时,就好像它是类型 (A*) 一样,我们在类 B 中创建的重写将被调用吗?
Having Class B that extends class A and overrides its functions can we be sure that when sending instance of (B*) as if it were type (A*) our overrides we created in class B will be called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要
A
上的方法被定义为虚拟,指针和引用都会出现这种情况。例如As long as the method on
A
is defined as virtual this will be the case for both pointers and references. For example是的,如果函数被声明为
虚拟
- 请参阅 http://www.parashift.com/c++-faq-lite/virtual-functions.htmlYes, if the functions are declared
virtual
- see http://www.parashift.com/c++-faq-lite/virtual-functions.html仅当 A 中的函数被声明为
虚拟
时。当声明为虚拟时,即使转换为父类,也会调用任何重写的子函数。Only if the function in A is declared
virtual
. When declaredvirtual
, any overriden child functions are called even when cast as a parent class.