<p>Is anyone aware of a language feature or technique in C++ to prevent a child class from over riding a particular method in the parent class?</p>

<pre><code>class Base {
public:
    bool someGuaranteedResult() { return true; }
};

class Child : public Base {
public:
    bool someGuaranteedResult() { return false; /* Haha I broke things! */ }
};
</code></pre>

<p>Even though it's not virtual, this is still allowed (at least in the Metrowerks compiler I'm using), all you get is a compile time warning about hiding non-virtual inherited function X.</p>

[{'answer_id': 17485, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 0, 'selected': False, 'text': "<p>If you address the child class as a type of its parent, then a non-virtual function will call the parent class's version.</p>\n\n<p>ie:</p>\n\n<pre><code>Parent* obj = new Child();\n</code></pre>\n"}, {'answer_id': 17486, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 5, 'selected': True, 'text': "<p>A couple of ideas:</p>\n\n<ol>\n<li>Make your function private.</li>\n<li>Do not make your function virtual. This doesn't actually prevent the function from being shadowed by another definition though.</li>\n</ol>\n\n<p>Other than that, I'm not aware of a language feature that will lock away your function in such a way which prevents it from being overloaded and still able to be invoked through a pointer/reference to the child class.</p>\n\n<p>Good luck!</p>\n"}, {'answer_id': 17487, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': -1, 'selected': False, 'text': '<p>C++ methods are private and un-overridable by default.</p>\n\n<ul>\n<li>You cannot override a private method</li>\n<li>You cannot override a non-<code>virtual</code> method</li>\n</ul>\n\n<p>Are you perhaps referring to overloading?</p>\n'}, {'answer_id': 17488, 'author': 'csmba', 'author_id': 350, 'author_profile': 'https://Stackoverflow.com/users/350', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>a compile time warning about hiding non-virtual inherited function X.</p>\n</blockquote>\n\n<p>change your compiler settings to make it a error instead of warning.</p>\n'}, {'answer_id': 17493, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 0, 'selected': False, 'text': '<p>Unless you make the method virtual, the child class cannot override it.  If you want to keep child classes from calling it, make it private.</p>\n\n<p>So by default C++ does what you want.</p>\n'}, {'answer_id': 17527, 'author': 'Danny Whitt', 'author_id': 375, 'author_profile': 'https://Stackoverflow.com/users/375', 'pm_score': 3, 'selected': False, 'text': '<p>Sounds like what you\'re looking for is the equivalent of the Java language <strong>final</strong> keyword that <a href="http://java.sun.com/docs/books/tutorial/java/IandI/final.html" rel="nofollow noreferrer">prevents a method from being overridden by a subclass</a>.</p>\n\n<p>As <a href="https://stackoverflow.com/questions/17483/c-anyway-to-prevent-a-method-from-being-over-ridden-in-sub-classes#17487">others</a> here have <a href="https://stackoverflow.com/questions/17483/c-anyway-to-prevent-a-method-from-being-over-ridden-in-sub-classes#17486">suggested</a>, you really can\'t prevent this.  Also, it seems that this is a rather <a href="http://www.google.com/search?q=c%2B%2B+prevent+override+method+like+java+final" rel="nofollow noreferrer">frequently asked question</a>.</p>\n'}, {'answer_id': 19192, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 0, 'selected': False, 'text': "<p>Trying to prevent someone from using the same name as your function in a subclass isn't much different than trying to prevent someone from using the same global function name as you have declared in a linked library. </p>\n\n<p>You can only hope that users that mean to use your code, and not others', will be careful with how they reference your code and that they use the right pointer type or use a fully qualified scope.</p>\n"}, {'answer_id': 120547, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>(a) I dont think making function private is the solution because that will just hide the base class function from the derived class.The derived class can always define a new function with the same signature.\n(b) Making the function non virtual is also not a complete solution because, if the derived class redefines the same function , one can always call the derived class function by compile time binding i.e obj.someFunction() where obj is an instance of the derived class.</p>\n\n<p>I dont think there is a way of doing this.Also,i would like to know  the reason for your decision to prohibit derived classes from overriding base class functions.</p>\n'}, {'answer_id': 120830, 'author': 'Luc Hermitte', 'author_id': 15934, 'author_profile': 'https://Stackoverflow.com/users/15934', 'pm_score': 0, 'selected': False, 'text': "<p>In your example, no function is overridden. It is instead hidden (it is a kind of degenerated case of overloading).\nThe error is in the Child class code. As csmba suggested, all you can do is changing your compiler settings (if possible) ; it should be fine as long as you don't use a third party library that hides its own functions.</p>\n"}, {'answer_id': 120891, 'author': 'RuntimeException', 'author_id': 15789, 'author_profile': 'https://Stackoverflow.com/users/15789', 'pm_score': 2, 'selected': False, 'text': '<p>I guess what the compiler warns you about is hiding !! Is it actually being overridden ? </p>\n\n<p>compiler might give you a warning, but at runtime, the parent class method will be called if the pointer is of type parent class, regardless of the actual type of the object it points to.  </p>\n\n<p>This is interesting. Try making a small standalone test program for your compiler. </p>\n'}, {'answer_id': 1561974, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>For clarification, most of you misunderstood his question. He is not asking about "overriding" a method, he is asking whether there is a way to prevent "hiding" or not. And the simple answer is that "there is none!".</p>\n\n<p>Here\'s his example once again</p>\n\n<p>Parent class defines a function: </p>\n\n<pre><code>int foo() { return 1; }\n</code></pre>\n\n<p>Child class, inheriting the Parent defines the same function AGAIN (not overriding):</p>\n\n<pre><code>int foo() { return 2; }\n</code></pre>\n\n<p>You can do this on all programming languages. There is nothing to prevent this code from compiling (except a setting on the compiler). The best you\'ll get is a warning that you are hiding the parent\'s method. If you call the child class and invoke the foo method, you\'ll get 2. You have practically broken the code.</p>\n\n<p>This is what he is asking.</p>\n'}, {'answer_id': 1891102, 'author': 'Vlad Sakharuk', 'author_id': 229992, 'author_profile': 'https://Stackoverflow.com/users/229992', 'pm_score': 0, 'selected': False, 'text': '<p>Technically u can prevent virtual functions to be be overridden. But you will never ever been able to change or add more. That is not help full. Better to use comment in front of function as faq lite suggests.</p>\n'}, {'answer_id': 16906116, 'author': 'moooeeeep', 'author_id': 1025391, 'author_profile': 'https://Stackoverflow.com/users/1025391', 'pm_score': 5, 'selected': False, 'text': '<p>When you can use the <code>final</code> specifier for virtual methods (introduced with C++11), you can do it. Let me quote <a href="http://en.cppreference.com/w/cpp/language/final" rel="noreferrer">my favorite doc site</a>:</p>\n\n<blockquote>\n  <p>When used in a virtual function declaration, final specifies that the function may not be overridden by derived classes.</p>\n</blockquote>\n\n<p>Adapted to your example that\'d be like:</p>\n\n<pre><code>class Base {\npublic:\n    virtual bool someGuaranteedResult() final { return true; }\n};\n\nclass Child : public Base {\npublic:\n    bool someGuaranteedResult() { return false; /* Haha I broke things! */ }\n};\n</code></pre>\n\n<p>When compiled:</p>\n\n<pre><code>$ g++ test.cc -std=c++11\ntest.cc:8:10: error: virtual function ‘virtual bool Child::someGuaranteedResult()’\ntest.cc:3:18: error: overriding final function ‘virtual bool Base::someGuaranteedResult()’\n</code></pre>\n\n<p>When you are working with a Microsoft compiler, also have a look at the <a href="https://msdn.microsoft.com/en-us/library/0w2w91tf(v=vs.140).aspx" rel="noreferrer"><code>sealed</code></a> keyword.</p>\n'}, {'answer_id': 38759520, 'author': 'Abhay Bhave', 'author_id': 5974168, 'author_profile': 'https://Stackoverflow.com/users/5974168', 'pm_score': -1, 'selected': False, 'text': '<p>I was searching for same and yesterday came to this [rather old] question.</p>\n\n<p>Today I found a neat c++11 keyword : <code>final</code> . I thought it may be useful for next readers.</p>\n\n<p><a href="http://en.cppreference.com/w/cpp/language/final" rel="nofollow">http://en.cppreference.com/w/cpp/language/final</a></p>\n'}]
['https://Stackoverflow.com/questions/17483', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1366/']