Topic: Different return type for redefined method


Author: tamlin@algonet.Sweden (Mike Nordell)
Date: 2000/03/22
Raw View
Steve Clamage wrote:

>
>Michael Forster wrote:
>>
>> Is this code standard compliant?
>>
>>     class A {};
>>     class B : public A {};
>>
>>     class X {
>>         A m() { return A(); }
>>     };
>>     class Y : public X {
>>         B m() { return B(); }
>>     };
>
>No, but the standard allows the return types to be A*/B* or A&/B&
>respectively, instead of A/B.
>
>Not all current compilers support this language feature, known as
>"covariant return types."
>
>Refer to section 10.3 paragraph 5 in the standard for full details.

Covariant return types are only mentioned/usable/allowed on virtual
member functions.

The code this user posted is not using virtual functions, and is AFIK
compliant. Y::m just hides X::m.


--
Mike
'Sweden' => 'se' *if* you reply by mail.

---
[ 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/03/23
Raw View

Mike Nordell wrote:
>
>
> >Refer to section 10.3 paragraph 5 in the standard for full details.
>
> Covariant return types are only mentioned/usable/allowed on virtual
> member functions.
>
> The code this user posted is not using virtual functions, and is AFIK
> compliant. Y::m just hides X::m.

Not only that but is return types are not references or pointers, so
if he did make the function virtual, it wouldn't be legal.

---
[ 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: "Martijn Lievaart" <news-from@greebo.orion.nl>
Date: 1999/12/11
Raw View
Michael Forster wrote in message
<384e2898@news.rz.uni-passau.de>...
>
>(c) This code is correct:
>
>    class A {};
>    class B : public A {};
>
>    class X {
>        A a;
>        virtual A& m() { return a; }
>    };
>    class Y : public X {
>        B b;
>        B& m() { return b; }
>    };
>
>Right? (I am asking because MSVC++ does not like code (c)).
>

Right. MSVC (5.0 dunno about 6) doesn't implement this (yet).

HTH
Martijn
--
Please post replies to this newsgroup. If you must reach
me by email, use <newsgroup-name> at greebo.orion in nl.
Senders of unsolicited bulk or commercial email will be
prosecuted
to the maximal extent possible by law and any other means.
---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/08
Raw View
Hyman Rosen wrote:
>
> Steve Clamage <stephen.clamage@sun.com> writes:
> > No, but the standard allows the return types to be A*/B* or A&/B&
> > respectively, instead of A/B.
> > Not all current compilers support this language feature, known as
> > "covariant return types."
> > Refer to section 10.3 paragraph 5 in the standard for full details.
>
> His functions were not virtual. It's a case of simple hiding,
> so the return types shouldn't matter.

Ah, yes. In my enthusiasm to talk about covariant return types,
I failed to notice that the functions weren't virtual.

There are no special requirements on functions in derived classes
unless they duplicate the name and parameter list of a virtual
function in a base class.

--
Steve Clamage, stephen.clamage@sun.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: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/12/08
Raw View
"Michael Forster" <forster@fmi.uni-passau.de> wrote in message
news:384d2bc7@news.rz.uni-passau.de...
> Hi,
>
> Is this code standard compliant?
>
>     class A {};
>     class B : public A {};
>
>     class X {
>         A m() { return A(); }
>     };
>     class Y : public X {
>         B m() { return B(); }
>     };
>
> (Look at the return types of X::m() and Y::m())

The code is complient. In your example above, function Y::m() hides X::m().
That is not what you probably wanted. In case your question was about
covariant return types then you should have made the functions virtual and
return pointer or reference to the objects.

--
Gene Bushuyev
Entia non sunt multiplicanda praeter necessitatem
---
[ 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: "Michael Forster" <forster@fmi.uni-passau.de>
Date: 1999/12/08
Raw View

Gene Bushuyev <gbush@my-deja.com> schrieb in im Newsbeitrag:
s4r1v1sdqeg67@corp.supernews.com...
> "Michael Forster" <forster@fmi.uni-passau.de> wrote in message
> news:384d2bc7@news.rz.uni-passau.de...
> > Hi,
> >
> > Is this code standard compliant?
> >
> >     class A {};
> >     class B : public A {};
> >
> >     class X {
> >         A m() { return A(); }
> >     };
> >     class Y : public X {
> >         B m() { return B(); }
> >     };
> >
> > (Look at the return types of X::m() and Y::m())
>
> The code is complient. In your example above, function Y::m() hides
X::m().
> That is not what you probably wanted. In case your question was about
> covariant return types then you should have made the functions virtual and
> return pointer or reference to the objects.

Thank you for all the answers, I think, I got the point. The result is

(a) My code is correct.
(b) This code is incorrect

    class A {};
    class B : public A {};

    class X {
        virtual A m() { return A(); }
    };
    class Y : public X {
        B m() { return B(); }
    };

(c) This code is correct:

    class A {};
    class B : public A {};

    class X {
        A a;
        virtual A& m() { return a; }
    };
    class Y : public X {
        B b;
        B& m() { return b; }
    };

Right? (I am asking because MSVC++ does not like code (c)).

Thank you very much,
Mike




[ 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: "Michael Forster" <forster@fmi.uni-passau.de>
Date: 1999/12/07
Raw View
Hi,

Is this code standard compliant?

    class A {};
    class B : public A {};

    class X {
        A m() { return A(); }
    };
    class Y : public X {
        B m() { return B(); }
    };

(Look at the return types of X::m() and Y::m())

Regards, Mike
---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/07
Raw View
Michael Forster wrote:
>
> Is this code standard compliant?
>
>     class A {};
>     class B : public A {};
>
>     class X {
>         A m() { return A(); }
>     };
>     class Y : public X {
>         B m() { return B(); }
>     };

No, but the standard allows the return types to be A*/B* or A&/B&
respectively, instead of A/B.

Not all current compilers support this language feature, known as
"covariant return types."

Refer to section 10.3 paragraph 5 in the standard for full details.

--
Steve Clamage, stephen.clamage@sun.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: Hyman Rosen <hymie@prolifics.com>
Date: 1999/12/07
Raw View
Steve Clamage <stephen.clamage@sun.com> writes:
> No, but the standard allows the return types to be A*/B* or A&/B&
> respectively, instead of A/B.
> Not all current compilers support this language feature, known as
> "covariant return types."
> Refer to section 10.3 paragraph 5 in the standard for full details.

His functions were not virtual. It's a case of simple hiding,
so the return types shouldn't matter.


[ 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              ]