Is there a way to call an object's base class method that's overriden? (C++)

后端 未结 3 1431
野性不改
野性不改 2021-01-22 04:27

I know some languages allow this. Is it possible in C++?

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 05:06

    Yes, just specify the type of the base class.

    For example:

    #include 
    
    struct Base
    {
        void func() { std::cout << "Base::func\n"; }
    };
    
    struct Derived : public Base
    {
        void func() { std::cout << "Derived::func\n"; Base::func(); }
    };
    
    int main()
    {
        Derived d;
        d.func();
    }
    

提交回复
热议问题