C++
--
Questions
--
Followers
Top Experts
in java we have final methods (functions) that cannot be overriden in a hierarchy.
How could that be done in C++?. Does C++ have a mechanism defined to do that?
I have read several places and cannot find a solution.
-prain
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.12
class FinalHelper {
private:
   friend class FinalClass;
   FinalHelper() { }
};
class FinalClass : private virtual FinalHelper {
public:
   FinalClass(){}
};
 int main(int argc, char* argv[])
{
   FinalClass finalclass;






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
FYI:
If you make your base class function non-virtual, then that function will be called if a base pointer is being used.
It will do so even if the derived class over writes the function.
However, if you use a pointer of the derived type, then the derive function is used.
You could try getting the desired requirements by using a final class using above method I posted, and use it in conjunction with you're class.
Your class can have a member object that has the final logic you want, and the final member object can be a proxy for the logic you want in your final member method.
  1) don't make it virtual
  2) don't override the method
There's no real need for a final keyword

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
C++
--
Questions
--
Followers
Top Experts
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.