What does the virtual keyword mean when overriding a method?

前端 未结 4 888
生来不讨喜
生来不讨喜 2021-01-11 12:16

What does the keyword virtual do when overriding a method? I\'m not using it and everything works fine.

Does every compiler behave the same in this

4条回答
  •  情话喂你
    2021-01-11 13:03

    When a function is virtual, it remains virtual throughout the hierarchy, whether or not you explicitly specify each time that it is virtual. When overriding a method, use virtual in order to be more explicit - no other difference :)

    class A
    {
        virtual void f() 
        {
          /*...*/
        };
    };
    
    class B:public A;
    {
        virtual void f()  //same as just void f()
        {
            /*...*/
        };
    };
    

提交回复
热议问题