[英]Calling Parent class method, from an object pointer?
Assuming I have a class B derived from A 假设我有一个来自A的B类
A defined foo() and also, B defines foo() 定义的foo()以及B定义foo()
I have a method in another class that receives a B* (pointer to B). 我在另一个类中有一个接收B *(指向B的指针)的方法。 In that function, can I call the A::foo(), just by using the pointer to B the function received? 在那个函数中,我可以调用A :: foo(),只需使用指向B的指针接收函数吗?
I think the following would do what you want: 我认为以下会做你想要的:
void fun(B* b){
b->A::foo();
}
Or, of course, you can do it with type casting: 或者,当然,您可以使用类型转换来执行此操作:
((A*)b)->foo();
or in c++ style with safety check: 或带有安全检查的c ++风格:
dynamic_cast<A*>(b)->foo();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.