After bashing Java so
brutally,
I have to add another thing to the list of Java features that I wish
C++ (or even just a GCC extension) had: @override
notation. Imagine this
scenario...
You have a base class that implements a bunch of methods, and some of
the methods have numerous overloads. We'll call this class base
.
You have a bunch of classes that derive from base
. Each of the
derived classes only override some of the methods provided by base
.
These derived classes are used by other infrastructure that just takes
pointers to objects of type base
.
For example:
class base {
virtual void do_something(float x);
virtual void do_something(int x);
virtual void do_something(char *str);
};
class d1 : public base {
virtual void do_something(float x);
};
class d2 : public base {
virtual void do_something(int x);
};
class d3 : public base {
virtual void do_something(char *str);
};
Everyone with me so far? Sound like every large C++ you've ever seen? Good.
Now the signature of some of the methods in base
changes, perhaps by
adding a parameter. While making that change, n-1 of the derived classes are
correctly modified.
class base {
virtual void do_something(float x, int y);
virtual void do_something(int x, int y);
virtual void do_something(char *str, int y);
};
class d1 : public base {
virtual void do_something(float x, int y);
};
class d2 : public base {
virtual void do_something(int x, int y);
};
class d3 : public base {
virtual void do_something(char *str /* FAIL!!! */ );
};
Try to find the bug or go to drinking island?
In Java this bug would never happen. All of the do_something
methods in the derived classed would be annotated with @override
.
When the compiler found do_something
in d3
that didn't match the
signature of any do_something
in base
, it would complain. With
C++ you just spend hours trying to track down the bug.
P.S.: I still hate Java.
I hate Java too...
With MSVC you can enable L4 warning C4263 you'll get compiler warning in that particular case.
http://msdn.microsoft.com/en-us/library/ay4h0tc9%28VS.80%29.aspx
c++0x has a similar feature. I'm not entirely keen on how they decided to go about it, but it's coming none-the-less.
You have to use annotations and mark up not only the methods, but also the classes to indicate that you want those checks to be applied.
The usual argument for those kinds of kludges is backwards compatibility. I've never understand why they don't just add #pragma stdver 99 commands to toggle the set of keywords the lexer will match and to alter other easy to switch behavior. More or less the same thing GLSL does. Oh well.
-Woverloaded-virtual
, that's almost what I want. It gives warnings about the legitimate overrides as well.The feature is described in proposal N2928:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2928.htm
I don't particularly like the syntax, but the feature is useful.
[[ ]]
notation for attributes is a bit on the ugly side, but it does seem to do the job. On the plus side, having a standardized attribute syntax will give cbloom one less thing to complain about.