Topic: override" keyword for derived classes?
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/04/09 Raw View
Michael Cook (mcook@cognex.com) wrote:
: >>>>> "kitk" == Kit Kauffmann <kitk@mudshark.sunquest.com> writes:
: >> His suggestion of the "override" keyword which cause this:
: kitk> What's wrong with pure virtual methods?
: 1. Perhaps you didn't want the class to incur the space overhead of a virtual
: table, or
: 2. Perhaps you didn't want the class to incur the time overhead of the virtual
: function calls, or
Under what circumstances would he want the function to always be
overidden but not want a virtual fucntion call?
: 3. Perhaps you didn't want the semantics of a virtual function call (perhaps
: you wanted static function selection instead of dynamic), or
Then what's the point of overriding the function?
: 4. Perhaps you wanted to tell the compiler precisely what you're trying to do
: instead of inventing yet another idiom.
This is the _purpose_ of pure virtual functions.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/04/10 Raw View
Robert Coie (rac@intrigue.com) wrote:
: I believe you have suggested the _opposite_ of what the original poster
: desired. He wanted the compiler to complain if a there was no virtual
: function with the same signature as a function in a derived class declared
: "override". Pure virtuals are constraints made in the base class: he
: wanted a constraint in the derived class. I was under the impression from
: the original post that X::foo() was a perfectly legitimate function.
You can still define a function if it is pure virtual:
class A
{
public:
virtual void foo() = 0; // subclass must define
};
class B : public A
{
public:
// I want my own but subclass should still do it
virtual void foo() = 0;
};
void A::foo()
{
// whatever
}
void B::foo()
{
// whatever - possibly including 'A::foo();'
}
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: franklin@millenium.texas.net (Frank Schmidt)
Date: 1995/04/10 Raw View
Calvin Clark (calvin@charlesview.com) wrote:
: I'm posting on behalf of a friend who has an interesting suggestion:
: he wants an "override" keyword in C++ which would be used to label
: functions in derived classes which *must* override the function of the
: same name in the parent class. Consider the following example. Let's
: say you intend to do this:
: class X
: {
: ...
: virtual int foo ();
: ...
: }
: class Y : public X
: {
: ...
: int foo ();
: ...
: }
: But instead you accidentally get the capitalization wrong on foo.
: (This happens a lot to programmers who use studly caps and excessively
: long function names---a pratice I ahbor but some of my coworkers
: gleefully embrace.)
: class Y : public X
: {
: ...
: int Foo ();
: ...
: }
: It might take a long time to figure out that Y::Foo() is never being
: called if you are calling Y::foo() in your code.
: His suggestion of the "override" keyword which cause this:
: class Y : public X
: {
: ...
: override int Foo ();
: ...
: }
: to generate a compile-time error if there is no X::Foo(). That
: way you'd know immediately that you'd done something wrong.
: I think this is a good suggestion, but I'm wondering if there's a good
: way to get this effect without adding a keyword to C++.
: -Calvin
There isn't. I think this is a good suggestion, but I would take it a
step further. The same kind of problem can occur when one wants to
write a new function but accidentally overides a function in a base
class. It would be nice to have 2 new keywords to solve this. One
could use "declare" and "redeclare" (like "override") as follows:
class X : public W
{
...
declare virtual int foo ();
...
}
class Y : public X
{
...
redeclare int foo ();
...
}
This makes it clear what is going on and allows the compiler to offer
more helpful error checking.
Author: Michael Cook <mcook@cognex.com>
Date: 1995/04/08 Raw View
>>>>> "kitk" == Kit Kauffmann <kitk@mudshark.sunquest.com> writes:
>> His suggestion of the "override" keyword which cause this:
kitk> What's wrong with pure virtual methods?
1. Perhaps you didn't want the class to incur the space overhead of a virtual
table, or
2. Perhaps you didn't want the class to incur the time overhead of the virtual
function calls, or
3. Perhaps you didn't want the semantics of a virtual function call (perhaps
you wanted static function selection instead of dynamic), or
4. Perhaps you wanted to tell the compiler precisely what you're trying to do
instead of inventing yet another idiom.
M.
Author: rac@intrigue.com (Robert Coie)
Date: 1995/04/08 Raw View
In article <kitk.1687.0009586B@mudshark.sunquest.com>,
kitk@mudshark.sunquest.com (Kit Kauffmann) wrote:
: In article <CALVIN.95Apr4124423@ray.charlesview.com>
calvin@charlesview.com (Calvin Clark) writes:
:
: >I'm posting on behalf of a friend who has an interesting suggestion:
: >he wants an "override" keyword in C++ which would be used to label
: >functions in derived classes which *must* override the function of the
: >same name in the parent class. Consider the following example. Let's
: >say you intend to do this:
:
: >class X
: >{
: >...
: > virtual int foo ();
: >...
: >}
:
: >class Y : public X
: >{
: >...
: > int foo ();
: >...
: >}
:
: >But instead you accidentally get the capitalization wrong on foo.
: >(This happens a lot to programmers who use studly caps and excessively
: >long function names---a pratice I ahbor but some of my coworkers
: >gleefully embrace.)
:
: >class Y : public X
: >{
: >...
: > int Foo ();
: >...
: >}
:
: >It might take a long time to figure out that Y::Foo() is never being
: >called if you are calling Y::foo() in your code.
:
: >His suggestion of the "override" keyword which cause this:
:
: >class Y : public X
: >{
: >...
: > override int Foo ();
: >...
: >}
:
: >to generate a compile-time error if there is no X::Foo(). That
: >way you'd know immediately that you'd done something wrong.
:
: >I think this is a good suggestion, but I'm wondering if there's a good
: >way to get this effect without adding a keyword to C++.
:
: >-Calvin
:
: What's wrong with pure virtual methods?
:
: int Foo() = 0;
:
: forces the derived class to provide an int Foo() method...
:
: Kit Kauffmann - kitk@mudshark.sunquest.com
: AKA 73363,447 (Compu$erve)
:
: Finger me for my public key
I believe you have suggested the _opposite_ of what the original poster
desired. He wanted the compiler to complain if a there was no virtual
function with the same signature as a function in a derived class declared
"override". Pure virtuals are constraints made in the base class: he
wanted a constraint in the derived class. I was under the impression from
the original post that X::foo() was a perfectly legitimate function.
Although it's not exactly elegant, the following macro hackery may be of
some help, especially if you are already a fan of the "inherited"
proposal:
#define MUST_OVERRIDE( f ) &inherited::f;
struct A { virtual int foo(); };
struct B : A
{
typedef A inherited;
int foo();
};
int B::Foo()
{
MUST_OVERRIDE( Foo );
/* ... */
}
Robert Coie rac@intrigue.com
Implementor, Intrigue Corporation AppleLink: INTRIGUE
Author: calvin@charlesview.com (Calvin Clark)
Date: 1995/04/04 Raw View
I'm posting on behalf of a friend who has an interesting suggestion:
he wants an "override" keyword in C++ which would be used to label
functions in derived classes which *must* override the function of the
same name in the parent class. Consider the following example. Let's
say you intend to do this:
class X
{
...
virtual int foo ();
...
}
class Y : public X
{
...
int foo ();
...
}
But instead you accidentally get the capitalization wrong on foo.
(This happens a lot to programmers who use studly caps and excessively
long function names---a pratice I ahbor but some of my coworkers
gleefully embrace.)
class Y : public X
{
...
int Foo ();
...
}
It might take a long time to figure out that Y::Foo() is never being
called if you are calling Y::foo() in your code.
His suggestion of the "override" keyword which cause this:
class Y : public X
{
...
override int Foo ();
...
}
to generate a compile-time error if there is no X::Foo(). That
way you'd know immediately that you'd done something wrong.
I think this is a good suggestion, but I'm wondering if there's a good
way to get this effect without adding a keyword to C++.
-Calvin
Author: christen@das.harvard.edu (Jon Christensen)
Date: 1995/04/04 Raw View
In article <CALVIN.95Apr4124423@ray.charlesview.com> calvin@charlesview.com (Calvin Clark) writes:
> I'm posting on behalf of a friend who has an interesting suggestion:
> he wants an "override" keyword in C++ which would be used to label
> functions in derived classes which *must* override the function of the
> same name in the parent class. Consider the following example. Let's
> say you intend to do this:
>
> [...]
Make foo a pure virtual function in the base class:
class X
{
virtual int foo() = 0;
}
--
Jon Christensen "Get your facts first, then you can distort
christen@das.harvard.edu them as you please." -- Mark Twain
Author: mwillm@vnet.ibm.com (Matthieu WILLM)
Date: 1995/04/05 Raw View
In article <CALVIN.95Apr4124423@ray.charlesview.com>,
on 04 Apr 1995 16:44:23 GMT,
Calvin Clark <calvin@charlesview.com> writes:
>
>I'm posting on behalf of a friend who has an interesting suggestion:
>he wants an "override" keyword in C++ which would be used to label
>functions in derived classes which *must* override the function of the
>same name in the parent class. Consider the following example. Let's
>say you intend to do this:
>
>class X
>{
>....
> virtual int foo ();
>....
>}
>
>class Y : public X
>{
>....
> int foo ();
>....
>}
>
>But instead you accidentally get the capitalization wrong on foo.
>(This happens a lot to programmers who use studly caps and excessively
>long function names---a pratice I ahbor but some of my coworkers
>gleefully embrace.)
>
>class Y : public X
>{
>....
> int Foo ();
>....
>}
>
>It might take a long time to figure out that Y::Foo() is never being
>called if you are calling Y::foo() in your code.
>
>His suggestion of the "override" keyword which cause this:
>
>class Y : public X
>{
>....
> override int Foo ();
>....
>}
>
>to generate a compile-time error if there is no X::Foo(). That
>way you'd know immediately that you'd done something wrong.
>
>I think this is a good suggestion, but I'm wondering if there's a good
>way to get this effect without adding a keyword to C++.
>
>-Calvin
Hi,
The 'override' keyword is supported in the SOM/DSOM toolkit (IBM's CORBA
implementation). You can write your class definition in IDL :
ex :
interface MyClass
void MyMethod(in string test);
;
interface MyDerivedClass : MyClass
MyMethod : override;
;
You then run the SOM compiler on it to produce C++ bindings.
SOM allow many other things such as full runtime objects, language neutral
(you can develop a class in one language and use it with another), metaclass
support, distributed objects and so on....It is currently available on OS/2,
Windows and RS6000 AIX.
Hope this helps.
---------------------------------------------------------------------
Matthieu Willm I do *NOT* speak for IBM ...
<mwillm@vnet.ibm.com>
Author: calvin@charlesview.com (Calvin Clark)
Date: 1995/04/05 Raw View
In article <CHRISTEN.95Apr4135838@das.harvard.edu> christen@das.harvard.edu (Jon Christensen) writes:
Make foo a pure virtual function in the base class:
class X
{
virtual int foo() = 0;
}
Although this works if you're the one writing the base class, it
dosen't solve the problem if you're using someone else's base class
that you're not permitted to modify. Also, this only generates an
error when you try to instantiate the derived class. If you haven't
written any code that does a Y y or a new Y, you won't get a
compile-time error.
-Calvin
Author: rac@intrigue.com (Robert Coie)
Date: 1995/04/05 Raw View
In article <CHRISTEN.95Apr4135838@das.harvard.edu>,
christen@das.harvard.edu wrote:
: In article <CALVIN.95Apr4124423@ray.charlesview.com>
calvin@charlesview.com (Calvin Clark) writes:
:
: > I'm posting on behalf of a friend who has an interesting suggestion:
: > he wants an "override" keyword in C++ which would be used to label
: > functions in derived classes which *must* override the function of the
: > same name in the parent class. Consider the following example. Let's
: > say you intend to do this:
: >
: > [...]
:
: Make foo a pure virtual function in the base class:
:
: class X
: {
: virtual int foo() = 0;
: }
My understanding of the original post was that the opposite behavior is
required. A function declared with the override keyword would generate a
compile-time error if there is no virtual function with the same signature
to override. For example:
struct A { virtual void f() = 0; };
struct B : public A { override void f(); };
struct C : public B { override void F(); };
Assuming you have #define override somewhere, this should compile and C
can be instantiated. The override keyword would protect the programmer
from failing to notice that F fails to override f and instead declares an
unrelated function, which IMHO cannot be achieved using the current pure
virtual syntax.
: --
: Jon Christensen "Get your facts first, then you can distort
: christen@das.harvard.edu them as you please." -- Mark Twain
Robert Coie rac@intrigue.com
Implementor, Intrigue Corporation AppleLink: INTRIGUE
Author: kitk@mudshark.sunquest.com (Kit Kauffmann)
Date: 1995/04/06 Raw View
In article <CALVIN.95Apr4124423@ray.charlesview.com> calvin@charlesview.com (Calvin Clark) writes:
>I'm posting on behalf of a friend who has an interesting suggestion:
>he wants an "override" keyword in C++ which would be used to label
>functions in derived classes which *must* override the function of the
>same name in the parent class. Consider the following example. Let's
>say you intend to do this:
>class X
>{
>...
> virtual int foo ();
>...
>}
>class Y : public X
>{
>...
> int foo ();
>...
>}
>But instead you accidentally get the capitalization wrong on foo.
>(This happens a lot to programmers who use studly caps and excessively
>long function names---a pratice I ahbor but some of my coworkers
>gleefully embrace.)
>class Y : public X
>{
>...
> int Foo ();
>...
>}
>It might take a long time to figure out that Y::Foo() is never being
>called if you are calling Y::foo() in your code.
>His suggestion of the "override" keyword which cause this:
>class Y : public X
>{
>...
> override int Foo ();
>...
>}
>to generate a compile-time error if there is no X::Foo(). That
>way you'd know immediately that you'd done something wrong.
>I think this is a good suggestion, but I'm wondering if there's a good
>way to get this effect without adding a keyword to C++.
>-Calvin
What's wrong with pure virtual methods?
int Foo() = 0;
forces the derived class to provide an int Foo() method...
Kit Kauffmann - kitk@mudshark.sunquest.com
AKA 73363,447 (Compu$erve)
Finger me for my public key