Topic: C# style "override" keyword
Author: danielgutson@hotmail.com (danielgutson@hotmail.com)
Date: Tue, 21 Jan 2003 16:25:08 +0000 (UTC) Raw View
6805b3x001@sneakemail.com (Davide Bolcioni) wrote in message news:<3E062681.7090202@sneakemail.com>...
> Greetings,
>
> Chris Putnam wrote:
> > Has there been a discussion about adding an "override" keyword to C++? The
> > meaning would be this: This function is intended to be an override of a
> > virtual function, if none of the base classes has a virtual function with
> > this signature, issue a compile error.
>
> Since introducing a new keyword might be a problem, we might try with
> a different syntax in the tradition of " = 0":
For example " > 0", would require that the function MUST exist in some base class.
struct Base { virtual void f() = 0; };
struct Der : Base { virtual void f() > 0; };
Daniel.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jasonsh@microsoft.com ("Jason Shirk")
Date: Sun, 22 Dec 2002 07:11:29 +0000 (UTC) Raw View
""Chris Putnam"" <nospam42@attbi.com> wrote in message
news:1040259570.202390@hqnntp01.autodesk.com...
> Hmm... I don't know about most compilers, but the compiler I use for
> everyday work (VC7) doesn't seem to have this most excellent warning. I
> typically compile with maximum warnings enabled, and recieve no warnings
for
> this problem (even if compiler extensions are disabled). (just tried it
> out to make sure I've not gone completely daft)
>
> So perhaps this is more of a compiler problem than a language issue. I
> suppose if there was a warning in VC7, I could also use a #pragma to
convert
> that warning to an error, and achieve the result I was looking for. Sure
> would be nice...
>
> Chris
>
Try warnings C4263 and C4264. They aren't enabled at /W4, you have to use
one of:
/Wall - Enable all warnings
/w1 4263 /w1 4264 - To make them level 1, or use /we to make them
errors
#pragma
Jason Shirk
VC++ Compiler Team
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: 6805b3x001@sneakemail.com (Davide Bolcioni)
Date: Mon, 23 Dec 2002 23:29:37 +0000 (UTC) Raw View
Greetings,
Chris Putnam wrote:
> Has there been a discussion about adding an "override" keyword to C++? The
> meaning would be this: This function is intended to be an override of a
> virtual function, if none of the base classes has a virtual function with
> this signature, issue a compile error.
Since introducing a new keyword might be a problem, we might try with
a different syntax in the tradition of " = 0":
/////////////////////////
// Example
/////////////////////////
class BaseFromLibrary
{
public:
virtual void function1( int x );
};
class MyClass : public BaseFromLibrary
{
public:
void function1( int x ) >>= void BaseFromLibrary::function1(int x);
};
In the same vein, "<<=" might be introduced in declarations to
add compile-time "attributes".
Davide Bolcioni
--
There is no place like /home.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: llewelly <llewelly.@@xmission.dot.com>
Date: Tue, 24 Dec 2002 00:37:24 CST Raw View
6805b3x001@sneakemail.com (Davide Bolcioni) writes:
[snip]
> Since introducing a new keyword might be a problem, we might try with
> a different syntax in the tradition of " = 0":
A syntax I've always wished had been a reserved word.
>
> /////////////////////////
> // Example
> /////////////////////////
>
> class BaseFromLibrary
> {
> public:
> virtual void function1( int x );
> };
>
> class MyClass : public BaseFromLibrary
> {
> public:
> void function1( int x ) >>= void BaseFromLibrary::function1(int x);
I think that would be much more readable as:
void function1( int x ) std::overrides void BaseFromLibrary::function1(int x);
And it would not conflict with any existing conforming program's use
of 'overrides' as an identifier.
> };
[snip]
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rmaddox@isicns.com (Randy Maddox)
Date: Wed, 18 Dec 2002 17:18:21 +0000 (UTC) Raw View
nospam42@attbi.com ("Chris Putnam") wrote in message news:<1040168909.334046@hqnntp01.autodesk.com>...
> Has there been a discussion about adding an "override" keyword to C++? The
> meaning would be this: This function is intended to be an override of a
> virtual function, if none of the base classes has a virtual function with
> this signature, issue a compile error.
>
> It may not seem that useful at first, but I've worked with a lot of base
> classes provided by others, and it is always frustrating when the signature
> of a virtual function is changed without my knowledge. I don't mind the
> signature change itself, as much as I mind not finding out about it at
> compile time. The problem is that my old class continues to compile, so I
> have to wait until run time to determine that the overridden function isn't
> being called anymore (because it isn't an override of the new signature).
>
Please correct me if I'm wrong here, but don't most compilers give you
a warning when you accidentally hide, rather than override, a base
class virtual function? Maybe all you need to do is to turn up the
warning level during the build.
Randy.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: csklu_news@yahoo.com (CSklu)
Date: Thu, 19 Dec 2002 04:08:11 +0000 (UTC) Raw View
>>It may not seem that useful at first, but I've worked with a lot of base
>>classes provided by others, and it is always frustrating when the signature
>>of a virtual function is changed without my knowledge. I don't mind the
>>signature change itself, as much as I mind not finding out about it at
>>compile time. The problem is that my old class continues to compile, so I
>>have to wait until run time to determine that the overridden function isn't
>>being called anymore (because it isn't an override of the new signature).
>>
>
>
> Please correct me if I'm wrong here, but don't most compilers give you
> a warning when you accidentally hide, rather than override, a base
> class virtual function? Maybe all you need to do is to turn up the
> warning level during the build.
>
> Randy.
I think the situation he is getting is not that it is accidentally
hiding, but that it is no longer overriding so then the polymorphic
behaviour is gone (calling the new virtual function on a pointer to the
base class no longer calls his old virtual method on his derived class).
In essence he ends up declaring a whole new method with no corresponding
method in the base class.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: philippe_mori@hotmail.com ("Philippe Mori")
Date: Thu, 19 Dec 2002 15:40:54 +0000 (UTC) Raw View
Nice idea... This would allows the compiler to do more check and help ensure
that changes in a base class won't get unnoticed... in more situation that a
warning about hiding a function would do.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nospam42@attbi.com ("Chris Putnam")
Date: Thu, 19 Dec 2002 17:40:02 +0000 (UTC) Raw View
Hmm... I don't know about most compilers, but the compiler I use for
everyday work (VC7) doesn't seem to have this most excellent warning. I
typically compile with maximum warnings enabled, and recieve no warnings for
this problem (even if compiler extensions are disabled). (just tried it
out to make sure I've not gone completely daft)
So perhaps this is more of a compiler problem than a language issue. I
suppose if there was a warning in VC7, I could also use a #pragma to convert
that warning to an error, and achieve the result I was looking for. Sure
would be nice...
Chris
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nospam42@attbi.com ("Chris Putnam")
Date: Thu, 19 Dec 2002 17:52:23 +0000 (UTC) Raw View
Yes, CSklu, that's my main issue. The override keyword would allow the
compiler to differentiate between accidental hiding (due to a base class
change or typo in the derived signature) and intentional hiding (if you
don't use the override keyword) it could assume that the hiding is
intentional.
I double checked last night (to be sure that I've not gone completely daft),
and I don't get any warning for this in VC7 (the compiler I use at work).
Chris
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: allan_w@my-dejanews.com (Allan W)
Date: Thu, 19 Dec 2002 21:57:37 +0000 (UTC) Raw View
rmaddox@isicns.com (Randy Maddox) wrote
> > Please correct me if I'm wrong here, but don't most compilers give you
> > a warning when you accidentally hide, rather than override, a base
> > class virtual function? Maybe all you need to do is to turn up the
> > warning level during the build.
csklu_news@yahoo.com (CSklu) wrote in message news:<3E0129C0.60103@yahoo.com>...
> I think the situation he is getting is not that it is accidentally
> hiding, but that it is no longer overriding so then the polymorphic
> behaviour is gone (calling the new virtual function on a pointer to the
> base class no longer calls his old virtual method on his derived class).
> In essence he ends up declaring a whole new method with no corresponding
> method in the base class.
I think you are both saying the same thing.
struct Base {
virtual void Foo2(long);
virtual void Foo3(int);
};
struct Der1 {
virtual void Foo1(long); //A
virtual void Foo2(long); //B
virtual void Foo3(long); //C
};
In //A, we are declaring a new virtual function. No error here.
In //B, we are overriding an existing virtual function. No error here either.
In //C, we hide Base::Foo3 and declare a new virtual function.
This is legal, but might not be what was intended (especially if a
prior version of class Base had Foo3(long)).
Chris Putnam, please correct me if I'm wrong -- Chris Putnam wants to
prevent this error by ressurecting an ancient (way-pre-standard)
keyword "override".
struct Der2 {
virtual void Foo1(long); //D
override void Foo2(long); //E
override void Foo3(long); //F
};
Since //D is a new virtual function, it is declared exactly the same way
that we do it now.
//E is different -- it uses the keyword override to state that we
intentionally mean to override Base::Foo2(long). (He didn't say, but I
get the impression that the use of "override" would be optional here --
the form in //B would also be legal.)
The virtue of this keyword becomes apparent in //F. Here we also
state that we mean to override Base::Foo3(long). Since there is no
such function, the compiler must report a diagnostic. The programmer
can fix this error in one of two ways:
* If Der2::Foo3(long) really was meant to hide Base::Foo3(int),
then go ahead and remove the keyword "override" (or change it
to "virtual").
* If Der2::Foo3(long) was meant to override Base::Foo3(int),
then fix the function signature to match the one in Base.
Randy Maddox, please correct me if I'm wrong -- Randy Maddox says that
many compilers already report //C with a warning message anyway.
This means that most of us can already receive a diagnostic simply by
setting the compiler to the right warning level, and therefore we don't
need to ressurect this keyword to achieve the same effect.
Does that make more sense?
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nospam42@attbi.com ("Chris Putnam")
Date: Tue, 17 Dec 2002 23:58:05 +0000 (UTC) Raw View
Has there been a discussion about adding an "override" keyword to C++? The
meaning would be this: This function is intended to be an override of a
virtual function, if none of the base classes has a virtual function with
this signature, issue a compile error.
It may not seem that useful at first, but I've worked with a lot of base
classes provided by others, and it is always frustrating when the signature
of a virtual function is changed without my knowledge. I don't mind the
signature change itself, as much as I mind not finding out about it at
compile time. The problem is that my old class continues to compile, so I
have to wait until run time to determine that the overridden function isn't
being called anymore (because it isn't an override of the new signature).
Using abstract base classes can address the problem to some extent, but that
only works if you have control over the base classes themselves. If someone
else has provided the library, users of the library have no easy way to
protect their own code so that they at least know what signatures have
changed. It is possible to diff the headers, but it would be easier (and
safer) to let the compiler do the checking for you.
In general, I'm still dubious about C#, but the "override" keyword seems to
solve this problem nicely.
Chris
/////////////////////////
// Example
/////////////////////////
class BaseFromLibrary
{
public:
virtual void function1( int x );
};
class MyClass : public BaseFromLibrary
{
public:
override void function1( int x );
};
//////////////////////
// In a future rev of the library, the base signature is changed to add
another parameter:
//////////////////////
class BaseFromLibrary
{
public:
virtual void function1( int x, int y );
};
class MyClass : public BaseFromLibrary
{
public:
override void function1( int x ); <<<< Compile Error: "function1() not
found with this signature in any base class"
};
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]