简体   繁体   中英

Calling Parent class method, from an object pointer?

Assuming I have a class B derived from A

A defined foo() and also, B defines foo()

I have a method in another class that receives a B* (pointer to B). In that function, can I call the A::foo(), just by using the pointer to B the function received?

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:

dynamic_cast<A*>(b)->foo();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM