Topic: Declaring functions as derived
Author: Pete Becker <petebecker@acm.org>
Date: Fri, 17 Feb 2006 10:49:08 CST Raw View
Ed J wrote:
> Does the standard support any method to declare a virtual function
> overridden by a derived class as being derived?
>
> In this example:
> class A {
> virtual void doStuff();
> };
> class B : public A {
> void doStuff(); // overrides A::doStuff()
> };
> the programmer explicitly created B::doStuff() to override A::doStuff(), but
> if A:doStuff() is subsequently deemed unnecessary and designed out of the
> base class, B::doStuff() would become a simple isomorphic interface, and
> compiler would be happy.
>
If you work in an environment where this sort of thing is allowed to
happen, you have my sympathy. My advice is to look for work elsewhere,
because the code base is not adequately controlled. Removing a virtual
function is a major change in interface, which should only be done after
careful review and even then probably shouldn't be done.
--
Pete Becker
Roundhouse Consulting, Ltd.
---
[ 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: "Archmagus" <yuenhoe@hotmail.com>
Date: Fri, 17 Feb 2006 11:07:09 CST Raw View
To the best of my knowledge no such keyword exists in C++. In fact I
doubt if its necessary. Polymorphic and isomorphic functions behave
similarly as long as ur using an object that is unambiguosly of the
derived class, as in using ur example :
B b; // derived type, no question
b.doStuff(); // no probs at all, whether the function is isomorphic or
virtual
This would work whether or not the base class A contains a virtual
doStuff method. Under polymorphic circumstances, ie if you have:
A* a;
a = new b; // upcast!
a->doStuff(); // polymorphic call
This would work perfectly if virtual A::doStuff() is around. If it were
deemed unecessary and removed from the base class, doStuff(), as u say,
will no longer part of the interface defined by the base class. The
code above will thus give u a compile time error.
---
[ 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: Michiel.Salters@tomtom.com
Date: Fri, 17 Feb 2006 13:34:02 CST Raw View
"Ed J" wrote:
> Does the standard support any method to declare a virtual function
> overridden by a derived class as being derived?
>
> In this example:
> class A {
> virtual void doStuff();
> };
> class B : public A {
> void doStuff(); // overrides A::doStuff()
> };
> the programmer explicitly created B::doStuff() to override A::doStuff(), but
> if A:doStuff() is subsequently deemed unnecessary and designed out of the
> base class, B::doStuff() would become a simple isomorphic interface, and
> compiler would be happy.
What works on current compilers is (in the implementation of B):
if (false) A::doStuff();
which can be encapsulated in a macro, of course.
#define OVERRIDES(X) if (0) { X(); }
OVERRIDES(A::doStuff)
HTH,
Michiel Salters
---
[ 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: Dietmar Kuehl <dietmar_kuehl@yahoo.com>
Date: Fri, 17 Feb 2006 14:38:21 CST Raw View
Pete Becker wrote:
> Ed J wrote:
>> Does the standard support any method to declare a virtual function
>> overridden by a derived class as being derived?
No but there was some discussion on a feature like this at the last
committee meeting. I don't remember whether it was actually a
proposal or just a general discussion on what might be reasonable
to add to the language for avoiding certain kinds of problems.
>> the programmer explicitly created B::doStuff() to override A::doStuff(),
>> but if A:doStuff() is subsequently deemed unnecessary and designed out of
>> the base class, B::doStuff() would become a simple isomorphic interface,
>> and compiler would be happy.
>>
>
> If you work in an environment where this sort of thing is allowed to
> happen, you have my sympathy. My advice is to look for work elsewhere,
> because the code base is not adequately controlled. Removing a virtual
> function is a major change in interface, which should only be done after
> careful review and even then probably shouldn't be done.
I completely agree with Pete although this is just one of the
scenarios where it would be desirable to verify that a function
really overrides a base class function. More than once I hunted
down bugs which resulted from an omitted 'const' or a mistyped
function name and thus a supposedly overriding function did not
do so. An error message like "function does not overrides a
virtual function at line ..." would be highly appreciated. On the
other hand, this feature does not have a high priority on my
personal wishlist.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
---
[ 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: "Ed J" <jed@privacy.net>
Date: Fri, 17 Feb 2006 16:16:37 CST Raw View
Pete Becker wrote:
> Removing a virtual function is a major change in interface, which should
> only be done after careful review and even then probably shouldn't be
done.
I agree, but I have usually encountered the problem when I am myself
designed a new hierarchy of classes. If I add a pure virtual function to
the base class, the build will fail until I fix the derived classes. The
compiler is giving me guidance on where to make changes. If I later realize
that I botched the factoring, unwinding the effects is not as easy because
deletion of the base class function doesn't have the same ripple effect on
the derived classes as had inventing it. I can't expect any guidance from
the compiler.
As you say, removing a virtual function is a major change. Adding one is a
less major change, so if the language supports additions by providing a
means (pure virtual) to mandate derivates, it would seem natural to provide
an equivalent means to mandate ancestry.
Ed J
---
[ 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: invalid@bigfoot.com (Bob Hairgrove)
Date: Sat, 18 Feb 2006 04:18:03 GMT Raw View
On Fri, 17 Feb 2006 14:38:21 CST, Dietmar Kuehl
<dietmar_kuehl@yahoo.com> wrote:
>Pete Becker wrote:
>> Ed J wrote:
>>> Does the standard support any method to declare a virtual function
>>> overridden by a derived class as being derived?
>
>No but there was some discussion on a feature like this at the last
>committee meeting. I don't remember whether it was actually a
>proposal or just a general discussion on what might be reasonable
>to add to the language for avoiding certain kinds of problems.
>
>>> the programmer explicitly created B::doStuff() to override A::doStuff(),
>>> but if A:doStuff() is subsequently deemed unnecessary and designed out of
>>> the base class, B::doStuff() would become a simple isomorphic interface,
>>> and compiler would be happy.
>>>
>>
>> If you work in an environment where this sort of thing is allowed to
>> happen, you have my sympathy. My advice is to look for work elsewhere,
>> because the code base is not adequately controlled. Removing a virtual
>> function is a major change in interface, which should only be done after
>> careful review and even then probably shouldn't be done.
>
>I completely agree with Pete although this is just one of the
>scenarios where it would be desirable to verify that a function
>really overrides a base class function. More than once I hunted
>down bugs which resulted from an omitted 'const' or a mistyped
>function name and thus a supposedly overriding function did not
>do so. An error message like "function does not overrides a
>virtual function at line ..." would be highly appreciated. On the
>other hand, this feature does not have a high priority on my
>personal wishlist.
There exists at least one compiler which does issue a warning about
this (Borland 5.5.1).
--
Bob Hairgrove
NoSpamPlease@Home.com
---
[ 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: "frege" <gottlobfrege@gmail.com>
Date: Sat, 18 Feb 2006 02:31:14 CST Raw View
"Ed J" wrote:
>
> Something like a "derived" keyword could be used by programmers to defined a
> function as an override, so a compiler could flag an error.
> For example:
> class B : public A {
> derived void doStuff(); // overrides A::doStuff()
> };
> Does such a capability exist in the language, or is it being considered?
>
Microsoft's compiler allows you to use 'A::' like so:
class B : public A {
void A::doStuff(); // explicitly override A
};
if there is not a matching doStuff in A, you get an error.
I think this is actually a rather nice way of doing it as it does not
require a new keyword.
Furthermore, I'd like to extend this to handle multiple inheritance
collisions (not sure if MS compiler does this part or not):
class C : public A, public B
{
void A::doStuff() // override A's doStuff
{
cout << "I'm being called as an A";
}
void B::doStuff() // separately override B's doStuff
{
cout << "I'm being called as a B";
}
}
I'd REALLY appreciate this. The collisions don't happen too often, but
when they do it is a major pain.
Tony
---
[ 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: "Greg Herlihy" <greghe@pacbell.net>
Date: Sun, 19 Feb 2006 16:51:01 CST Raw View
Dietmar Kuehl wrote:
> Pete Becker wrote:
> > Ed J wrote:
> >> Does the standard support any method to declare a virtual function
> >> overridden by a derived class as being derived?
>
> No but there was some discussion on a feature like this at the last
> committee meeting. I don't remember whether it was actually a
> proposal or just a general discussion on what might be reasonable
> to add to the language for avoiding certain kinds of problems.
There does appear to be a specific proposal (N1827) for an explicit
override syntax. The proposal can be found here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1827.htm
Greg
---
[ 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: jed@privacy.net ("Ed J")
Date: Fri, 17 Feb 2006 06:59:37 GMT Raw View
Does the standard support any method to declare a virtual function
overridden by a derived class as being derived?
In this example:
class A {
virtual void doStuff();
};
class B : public A {
void doStuff(); // overrides A::doStuff()
};
the programmer explicitly created B::doStuff() to override A::doStuff(), but
if A:doStuff() is subsequently deemed unnecessary and designed out of the
base class, B::doStuff() would become a simple isomorphic interface, and
compiler would be happy.
Something like a "derived" keyword could be used by programmers to defined a
function as an override, so a compiler could flag an error.
For example:
class B : public A {
derived void doStuff(); // overrides A::doStuff()
};
Does such a capability exist in the language, or is it being considered?
Ed J
---
[ 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: "perise" <perise@gmail.com>
Date: Fri, 17 Feb 2006 09:40:18 CST Raw View
no, there is no such character in c plus plus
---
[ 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 ]