0
votes

Derived1 and Derived2 inherit from Base while no classes inherit from Derived1 and Derived2. In the classes declared below, will I be able to reduce one level of indirection by not using the keyword 'virtual' in the declaration of the member function foo()? Or more specifically is the performance of the function Derived2::foo() be better than the performance of Derived1::foo()?

     class Base{
     public: 
     virtual void foo();
    }

    class Derived1: public Base{
      public:
      virtual void foo();
   }   

   class Derived2: public Base{
      public:
      void foo(); 
   }
2

2 Answers

2
votes

No, there is no difference.

The keyword virtual is optional in derived classes. If the function is virtual in the base class, it is virtual in all derived classes as well.

1
votes

No, since Base::foo is virtual it doesn't matter whether you use virtual in the derived classes. It will be a virtual function whether you use virtual keyword or not.