Topic: override (Was: Ideas for new keywords and features)


Author: postmast.root.admi.gov@iname.com (blargg)
Date: 1999/07/25
Raw View
In article <379A5DDC.1297@wanadoo.fr>, Valentin Bonnard
<Bonnard.V@wanadoo.fr> wrote:

> blargg wrote:
>
> > Of the two, "override" would provide all the safety in this
> > regard, since one couldn't accidentally override a virtual function
> > without knowing it.
>
> Requiring every OO C++ program to be rewritten to put override
> on every overrider.
>
> override is great, but making it mandatory is a bit too much IMO.

Yeah, practically it is not an option:

    struct B {
        virtual void f();
        virtual void g();
    };

    struct D : B {
        void f();         // illegal under proposed override rules
        virtual void g(); // illegal under proposed override rules
    };

I think a compiler which rejected currently correct code like the above
would be very unpopular :-)

What if, syntactically, to solve this, "overridable" was introduced, which
would be like virtual except it can only be used when introducing a "new"
virtual function:

    struct B {
        overridable void f();
        overridable void g();
        overridable void h();
    };

    struct D : B {
        void f();             // illegal
        override void g();    // OK
        overridable void h(); // illegal - h() already exists
    };

This would allow code using the new, safer constructs to peacefull
co-exist with old code that doesn't.

The error checking could even be done on a per-class basis, where using
the "override" modifier for an override would enable the new rules:

    struct B {
        virtual void f();
        virtual void g();
    };

    struct old : B
    {
        // doesn't use override - use current rules

        void f(); // OK
        void g(); // OK
    };

    struct new_rules : B
    {
        // uses override - use new safer overriding rules

        override void f(); // OK
        void g();          // error
    };

I have problems with this, though, beacuse of the uncertainty of checking.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/07/25
Raw View
blargg wrote:

> Of the two, "override" would provide all the safety in this
> regard, since one couldn't accidentally override a virtual function
> without knowing it.

Requiring every OO C++ program to be rewritten to put override
on every overrider.

override is great, but making it mandatory is a bit too much IMO.

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]