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.

comment 1

I hate Java too... :)

virtual void do_something(char str / FAIL!!! */ );

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

Comment by Branimir Karadzic Wed 18 Aug 2010 02:12:20 PM PDT
Static analysis
C++ does not have such a feature in itself, but you can enforce it with a static analysis tool like this one: https://bugzilla.mozilla.org/show_bug.cgi?id=500870
Comment by Arpad Borsos Wed 18 Aug 2010 02:37:10 PM PDT
c++0x

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.

Comment by Anonymous Wed 18 Aug 2010 02:47:43 PM PDT
RE: comment 1
GCC has a warning, -Woverloaded-virtual, that's almost what I want. It gives warnings about the legitimate overrides as well. :(
Comment by IanRomanick Wed 18 Aug 2010 03:55:35 PM PDT
Re: c++0x

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.

Comment by Luca Thu 19 Aug 2010 05:24:53 AM PDT
Re: c++0x
Yeah... the [[ ]] 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.
Comment by IanRomanick Tue 24 Aug 2010 06:18:15 PM PDT