Topic: virtual function return value override
Author: aviadr1@my-deja.com
Date: 2000/08/29 Raw View
Hi all,
I want to know why the following doesn't work:
(doesn't compile on MSVC 6.0)
//
//
class Base {
virtual Base* get ();
};
//
class Derived {
virtual Derived* get (); // overriding Base::Get ();
};
//
void main ()
{
Derived d;
Derived* anotherD = d.get(); // get () should return Derived*
Base* b = d.get (); // get () should still return Derived*
Base* anotherB = b->get (); // get () should return Base*
anotherD = anotherB->get (); // only this should produce an error
}
my reasoning is that returning a more derived type only strengthens
the contract without violating it, and therefore it should be possible
to override virtual functions with functions that have the same
signature exect their return type is more derived
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/08/29 Raw View
In article <8ofr65$hfj$1@nnrp1.deja.com>, aviadr1@my-deja.com writes
>Hi all,
>I want to know why the following doesn't work:
>(doesn't compile on MSVC 6.0)
>//
>//
>class Base {
> virtual Base* get ();
>};
>//
>class Derived {
> virtual Derived* get (); // overriding Base::Get ();
>};
>//
>void main ()
If you elect to use an MS extension (letting main return void) you
really have no reason to grumble because MS has elected to not implement
covariant return (as the Standard specifies)
The simple answer is to use a more conforming compiler from a vendor who
is less dedicated to the vendor lock-in anti-pattern :)
>{
> Derived d;
> Derived* anotherD = d.get(); // get () should return Derived*
> Base* b = d.get (); // get () should still return Derived*
> Base* anotherB = b->get (); // get () should return Base*
>
> anotherD = anotherB->get (); // only this should produce an error
>}
>
>my reasoning is that returning a more derived type only strengthens
>the contract without violating it, and therefore it should be possible
>to override virtual functions with functions that have the same
>signature exect their return type is more derived
>
>
>
>
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
>
>---
>[ 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 ]
>
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: Edward Diener <eddielee@abraxis.com>
Date: 2000/08/29 Raw View
VC++ 6.0 does not support covariant return types even though it is part of
the C++ standard. C++ Builder does support it and the fact that VC++ has not
supported it for a number of iterations of Microsoft's compiler always gets a
few laughs naturally on the Borland C++ Builder forums.
aviadr1@my-deja.com wrote:
> Hi all,
> I want to know why the following doesn't work:
> (doesn't compile on MSVC 6.0)
> //
> //
> class Base {
> virtual Base* get ();
> };
> //
> class Derived {
> virtual Derived* get (); // overriding Base::Get ();
> };
> //
> void main ()
> {
> Derived d;
> Derived* anotherD = d.get(); // get () should return Derived*
> Base* b = d.get (); // get () should still return Derived*
> Base* anotherB = b->get (); // get () should return Base*
>
> anotherD = anotherB->get (); // only this should produce an error
> }
>
> my reasoning is that returning a more derived type only strengthens
> the contract without violating it, and therefore it should be possible
> to override virtual functions with functions that have the same
> signature exect their return type is more derived
---
[ 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: Ron Natalie <ron@sensor.com>
Date: 2000/08/29 Raw View
aviadr1@my-deja.com wrote:
>=20
> class Derived {
> virtual Derived* get (); // overriding Base::Get ();
> };
Presumably Derived should be derived from base and you omitted it.
The problem is that Visual C++ is broken in this respect. The work-aroun=
d is
to just have Derived::get return Base*. This works fine in most cases.
> void main ()
Main must return INT, despite what VC++ lets you get away with.
> Derived d;
> Derived* anotherD =3D d.get(); // get () should return Derived*
This is the one case you're going to have to code around if you make Deri=
ved::get
return Base*:
Derived* anotherD =3D static_cast<Derived*>(d.get);
> my reasoning is that returning a more derived type only strengthens
> the contract without violating it, and therefore it should be possible
> to override virtual functions with functions that have the same
> signature exect their return type is more derived
Return type is NOT part of the signature. However, the standard allows t=
he
return type vary in this respect:
The return type of an overriding function shall be either identical to th=
e return type of the overridden func-tion
or covariant with the classes of the functions. If a function D::f overri=
des a function B::f, the
return types of the functions are covariant if they satisfy the following=
criteria:
=97 both are pointers to classes or references to classes 98)
=97 the class in the return type of B::f is the same class as the class i=
n the return type of D::f or, is an
unambiguous direct or indirect base class of the class in the return type=
of D::f and is accessible in D
=97 both pointers or references have the same cv-qualification and the cl=
ass type in the return type of D::f
has the same cv-qualification as or less cv-qualification than the class =
type in the return type of B::f.
---
[ 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: Anders Pytte <anders@milkweed.com>
Date: 2000/08/29 Raw View
in article 8ofr65$hfj$1@nnrp1.deja.com, aviadr1@my-deja.com at
aviadr1@my-deja.com wrote on 8/29/00 10:29 AM:
> Hi all,
> I want to know why the following doesn't work:
> (doesn't compile on MSVC 6.0)
> //
> //
> class Base {
> virtual Base* get ();
> };
> //
> class Derived {
Should this be
class Derived : public Base {
?
> virtual Derived* get (); // overriding Base::Get ();
> };
> //
> void main ()
> {
> Derived d;
> Derived* anotherD = d.get(); // get () should return Derived*
> Base* b = d.get (); // get () should still return Derived*
> Base* anotherB = b->get (); // get () should return Base*
>
> anotherD = anotherB->get (); // only this should produce an error
> }
>
> my reasoning is that returning a more derived type only strengthens
> the contract without violating it, and therefore it should be possible
> to override virtual functions with functions that have the same
> signature exect their return type is more derived
I think your reasoning is sound, but you have not told use where the problem
occurred. Did the compiler complain? If so, where?
Anders.
--
Anders Pytte Milkweed Software
PO Box 32 voice: (802) 586-2545
Craftsbury, VT 05826 email: anders@milkweed.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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/08/30 Raw View
Anders Pytte wrote:
>
>
>
> I think your reasoning is sound, but you have not told use where the problem
> occurred. Did the compiler complain? If so, where?
>
His compiler complains because Derived::get doesn't have the same return type
as the virtual Base::get it overrides. It's a defect in the compiler (VC++).
---
[ 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: "Richard Parkin" <rparkin@msi-eu.com>
Date: 2000/08/30 Raw View
<aviadr1@my-deja.com> wrote in message news:8ofr65$hfj$1@nnrp1.deja.com...
> Hi all,
> I want to know why the following doesn't work:
> (doesn't compile on MSVC 6.0)
<Snip covariant return example>
Simple, because VC6 doesn't support covariant returns.
Ric
---
[ 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 ]