C++ Virtual function being hidden

后端 未结 6 1345
粉色の甜心
粉色の甜心 2020-12-05 10:56

I\'m having a problem with C++ inheritance.

I have a class hierarchy:

class A {
public:
   virtual void onFoo() {}
   virtual void onFoo(int i) {}
};         


        
6条回答
  •  渐次进展
    2020-12-05 11:30

    Methods on class A and B should be public. That, and you are missing semi-colons at the end of each class declaration.

    class A {
    public:
       virtual void onFoo() {}
       virtual void onFoo(int i) {}
    };
    
    class B : public A {
    public:
        virtual void onFoo(int i) {}
    };
    
    class C : public B {
    };
    
    
    int main() {
        C* c = new C();
        c->onFoo(); //Compile error - doesn't exist
    }
    

提交回复
热议问题