View Full Version : [C++] opposite of 'virtual'
November 10th, 2009, 09:53 PM
I use virtual when I want to indicate that a particular member function is to be defined in derived classes. Is there a way I can do the opposite? Is there a way I can make a function immune to override/redefinition/etc.? Can I make it throw a compiler error if another class attempts to do that?
November 10th, 2009, 10:11 PM
There is no "final" in C++... but, a non-virtual method is about as good.
November 10th, 2009, 10:19 PM
There is no such thing. I'm not sure if you are familiar with Java's "final" statement, but here's a thread of some bloke looking to implement it in C++.
November 10th, 2009, 10:23 PM
That's what I figured. I am aware of Java's final keyword. I was hoping C++ would have a similar mechanic I may have missed.
November 10th, 2009, 11:49 PM
as said you can't lock a method from being overriden but you can forbid that class derives from another:
November 11th, 2009, 11:59 AM
Even if/when the "final" keyword gets into C++, it would only be a suggestion to other programmers. There are a dozen theoretical ways to overcome restrictions like that, including obtaining a copy of your source code and compiling it with "final" deleted!
IMO the most effective way currently is this:
// NOTE: Warning! Do *not* override or redefine this function!
// You will be fired if you do. No exceptions!
November 11th, 2009, 05:22 PM
Heh... well, yeah, with access to the source code, anything is possible (including nuking the entire thing and starting from scratch). I just figured it would be nicer for anyone attempting to override to get a compiler error that makes it abundantly clear that it should not be redefined. I'm not looking to physically restrain people. :P
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.