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

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

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

3条回答
  •  终归单人心
    2021-01-22 04:49

    Yes:

    #include 
    
    class X
    {
        public:
            void T()
            {
                std::cout << "1\n";
            }
    };
    
    class Y: public X
    {
        public:
            void T()
            {
                std::cout << "2\n";
                X::T();             // Call base class.
            }
    };
    
    
    int main()
    {
        Y   y;
        y.T();
    }
    

提交回复
热议问题