prain
asked on
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
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
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
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.12
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;
class FinalHelper {
private:
friend class FinalClass;
FinalHelper() { }
};
class FinalClass : private virtual FinalHelper {
public:
FinalClass(){}
};
int main(int argc, char* argv[])
{
FinalClass finalclass;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
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.
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.
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
1) don't make it virtual
2) don't override the method
There's no real need for a final keyword
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11