Topic: Q: Overriding functions -- what does the (proposed) standard say?
Author: xcaliber@netcom.com (Jerome Downey)
Date: Wed, 23 Nov 1994 17:11:39 GMT Raw View
Hi folks,
I have a question about overriding functions in derived classes, and what
their signature should be. Take a look at the following code --
struct base
{
virtual void f(base &x) = 0;
};
struct derived : public base
{
virtual void f(derived &x) {}
};
derived x; // error (?)
Note that the function f in derived is identical to that in base except for
the argument. According to the standard rules of function signature, the
signatures of the two function are different. Thus derived is also abstract
and instantiating it (as in the last statement) is illegal.
However, I've heard that the proposed standard has relaxed some of the signature
rules for virtual functions.
My question is, will the proposed standard accept the above code, ie; will f
in derived actually override f in base? How about the following --
struct base
{
virtual base &f() = 0;
};
struct derived : public base
{
virtual derived &f() {}
};
derived x; // error (?)
ie; functions differing only in return type; and that too related by
inheritance?
BTW, I've tried the first code fragment on MSVC++ 1.51 and it is not accepted.
Thanx.
_____________
vipul m. shah
--
Jerome Downey
XCaliber Trading Systems, Inc.
xcaliber@netcom.com
Author: mec@shell.portal.com (Michael Edward Chastain)
Date: 23 Nov 1994 20:39:43 GMT Raw View
In article <xcaliberCzqD3F.JEw@netcom.com>,
Jerome Downey <xcaliber@netcom.com> wrote:
> Note that the function f in derived is identical to that in base except for
> the argument. According to the standard rules of function signature, the
> signatures of the two function are different. Thus derived is also abstract
> and instantiating it (as in the last statement) is illegal.
Yes.
> However, I've heard that the proposed standard has relaxed some of the
> signature rules for virtual functions.
Yes. The proposed relaxation is on _return types_.
class Base { public: virtual Base * clone( ) const; };
class Derived : public Base { public: virtual Derived * clone( ) const; };
Every instance of 'Derived' is-a 'Base' So when 'Derived::clone' returns
a pointer to 'Derived', this return value is acceptable as a pointer to
Base, and can be used anywhere a pointer to base can.
Reference: Stroustrup, 'The C++ Programming Language', 2nd edition,
Appendix A (ANSI/ISO Resolutions), r.10.2.
> My question is, will the proposed standard accept the above code, ie; will f
> in derived actually override f in base?
No. In your code, the base class function will accept any ptr-to-base.
In order to be substitutable, the derived class function must accept
any argument of type ptr-to-base. But in your code, it accepts only
arguments of type ptr-to-derived.
I believe *this* code is type-safe, but it's not legal C++. I also don't
know any practical use for it.
class D;
class B { public: virtual void foo( const D & ); };
class D : public B { public: virtual void foo( const B & ); };
> How about the following --
>
> struct base
> {
> virtual base &f() = 0;
> };
>
> struct derived : public base
> {
> virtual derived &f() {}
> };
>
> derived x; // error (?)
>
> ie; functions differing only in return type; and that too related by
> inheritance?
Yes, this is now legal.
> BTW, I've tried the first code fragment on MSVC++ 1.51 and it is not
> accepted.
Some compilers are behind the evolving standard-in-progress.
Michael Chastain
mec@shell.portal.com