2 ms·
Um... isn't that guaranteed? What would it mean for a private function to be virtual? It can't be overridden by a different implementation in a child class...
by AnimalMuppet 3y ago
Um... isn't that guaranteed? What would it mean for a private function to be virtual? It can't be overridden by a different implementation in a child class...
- zetafunction 3y ago`final` prevents a child class from overriding a method. `private` does not.
- Findecanor 3y agoYes, you can have a private virtual member function, and it can be overridden in a child class (unless declared final), apparently. I too thought that sounded insane, so I just looked it up. I've been programming C++ for twenty five years and the thought of wanting to do this have never ever occurred to me...
- AnimalMuppet 3y agoTIL.
- usefulcat 3y agoIt could make sense if you have a method that you want derived classes to be able to override but not to be able to call directly. This is, admittedly, a pretty niche case but certainly not inconceivable.
- zetafunction 3y agoExcept the derived class can simply change the visibility of the override, so...
- bregma 3y agoChild classes can override private member functions in their parent class. Making all virtual functions private is the better way to implement inheritance: it separates implementation from interface, allows for common functionality to be moved to the base class without requiring gymnastics in each and every derived class, and even lets you simplify the public interface to reduce compile times. Try it, you'll like it. You may never go back to writing Java in C++.