How to call a parent class function from derived class function?

后端 未结 7 1191
自闭症患者
自闭症患者 2020-11-22 06:16

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is deri

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 06:57

    In MSVC there is a Microsoft specific keyword for that: __super


    MSDN: Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.

    // deriv_super.cpp
    // compile with: /c
    struct B1 {
       void mf(int) {}
    };
    
    struct B2 {
       void mf(short) {}
    
       void mf(char) {}
    };
    
    struct D : B1, B2 {
       void mf(short) {
          __super::mf(1);   // Calls B1::mf(int)
          __super::mf('s');   // Calls B2::mf(char)
       }
    };
    

提交回复
热议问题