简体   繁体   中英

how a derived class pointer can call the base class function

I have a base class 'a' which includes some virtual function , in the derived class 'b' i overload that function . Now I have one more class 'c' derived from 'b' only and I have a pointer to class c .But I am not able to figure out how to call that function from the 'c' class pointer . Help me out , maybe its stupid but I am stuck here .

struct A { virtual void foo() {} };
struct B: A { void foo() override {} };
struct C: B {};

auto main() -> int
{
    C o;
    C* p = &o;

    p->foo();    // Calls B::foo
    p->A::foo(); // Calls A::foo
}

如果c是指向C的指针:

c->A::method();

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