bisarch
bisarch

Reputation: 1398

Effect of virtual keyword with a function in the *derived* class on performance

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(); 
   }

Upvotes: 0

Views: 210

Answers (2)

Bo Persson
Bo Persson

Reputation: 92261

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.

Upvotes: 2

Naveen
Naveen

Reputation: 73443

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.

Upvotes: 1

Related Questions