Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual function declared non-virtual in a derived class

Tags:

c++

If a function is declared non-virtual in a derived class when the base class function was virutal, why does it invoke a vtable lookup on calling the function on its pointer? The function is clear from the scope.

like image 449
Anand Kumar Sinha Avatar asked Oct 13 '11 15:10

Anand Kumar Sinha


2 Answers

In C++ if you declare a method virtual in the base class then it's virtual also in derived class, even if the virtual keyword is omitted.

For documentation purposes is however in my opinion nice to repeat it anyway.

like image 133
6502 Avatar answered Oct 16 '22 06:10

6502


You cannot make a function non-virtual, so it will stay virtual and a call to the function is in general also virtual. Of course, there are situations where the compiler will be able to optimize this and do a direct call, but apparantly not in your scenario.

like image 37
Janick Bernet Avatar answered Oct 16 '22 06:10

Janick Bernet