Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of prain
prain🇺🇸

C++ final functions or methods
Hi,
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.


Avatar of jasonclarkejasonclarke

There is no 'built-in' method in C++ - this article answers your question in detail:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11


Sry didn't read it correctly - but in any case the following article in the FAQs talks about methods:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.12

Avatar of AxterAxter🇺🇸

Here's a method for creating a final class in C++:
class FinalHelper {
private:
      friend class FinalClass;
      FinalHelper() { }
};

class FinalClass : private virtual FinalHelper {
public:
      FinalClass(){}
};


 int main(int argc, char* argv[])
{
      FinalClass finalclass;

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of AxterAxter🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of AxterAxter🇺🇸

>>in java we have final methods (functions) that cannot be overriden in a hierarchy.

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.

Avatar of prainprain🇺🇸

ASKER

Thanks for all this. But still I cannot find the answer to my question. Some of you have given me how to create final classes. But that's not what I want. I still want to inherit, but I want to make some of the methods not allow to override (so make them final) in subclasses. In java you can do this very easily.

Avatar of Infinity08Infinity08🇧🇪

The answer is that there's no such thing in C++ as a final method. The best solution is to do these things for a method you want to be final :

    1) don't make it virtual
    2) don't override the method

There's no real need for a final keyword

Free T-shirt

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.


SOLUTION
Avatar of Infinity08Infinity08🇧🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
C++

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.