Why must I re-declare a virtual function from an inherited class?

前端 未结 4 1104
长发绾君心
长发绾君心 2021-02-14 02:24

I\'m working on a simple C++ program and am having a difficult time understanding a compiler error I was getting. The issue was caused by me attempting to create a derived class

4条回答
  •  终归单人心
    2021-02-14 03:19

    When you declare a method in a derived class, you say to the compiler:

    I want to override this method in this class

    So if you don't declare a method in the derived class, you say:

    I don't want to override this method; derived class's implementation is the same as the one in the base class

    In your case, the base class declares them as pure virtual, so in this case it can be paraphrased:

    I don't want to implement this method in this class

    If you try to define a method but not declare it, you contradict yourself. The compiler detects that (to protect you from your own negligence).

提交回复
热议问题