Is it possible to make my member functions final as in Java, so that the derived classes can not override them?
Is it possible to make my member functions final as in Java, so that
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is so much possible that it is in fact the default behaviour. I.e. if you don’t declare your class instance methods explicitly as
virtual, they can’t be overridden in subclasses (only hidden, which is a different – and almost always erroneous – case).Effective C++ Third Edition, Item 36 deals with this in detail. Consider
So this is not exactly how
finalbehaves in Java, but you can only get this close with C++. An alternative might be to prevent subclassing altogether. The technical – working, but not nice – solution to this is to declare all your constructorsprivate(and provide a static factory method if you want to allow instantiation of your class, of course).