Is a virtual final method better than not virtual in the root class?

I have an abstract class that is the root of the class hierarchy. This root class has a method with some trivial implementation, and it seems that there is no need to change this implementation anywhere at any time.

Make this method non-virtual in order, but there is a chance that some subclass will reimplement it randomly. Is a virtual finite method the best solution in this case?

+4
source share
2 answers

If you decide to declare the method as virtual... final, you will do everything to pay the cost of virtual sending at runtime to protect a small number of users who might accidentally hide the base class function.

Since one of the guiding principles in C ++ is: "you will not pay for what you are not using , I would say that it is better to leave the function non-virtual, and even if they hide this function.

+1
source

, , . , , ?, , , , .

0

All Articles