C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Return types of overridden functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Sat Feb 28, 2004 4:20 am    Post subject: Return types of overridden functions Reply with quote



Hello,

as we all know, a virtual function that is overridden in a derived class can
differ in its return type in as much as it can return a pointer to a class
that is derived from the class to which a pointer is returned in the base
class, as in the following example:

struct Base
{
virtual Base* Clone();
};

struct Derived : public Base
{
virtual Derived* Clone(); // Returns a Derived* instead of a Base*
};

However, does this only apply to pointers (or references)? What if the
function returns an object, like so:

struct Base
{
virtual Base f(); // Returns an object, not a pointer.
};

struct Derived : public Base
{
virtual Derived f(); // Returns a derived object, but not a pointer either.
};

It will obviously lead to object slicing, as in the following example:

int main()
{
Base* p = new Derived;

// Calls Derived::f(), returning an object of type "Derived".
// Constructs a Base from a Derived.
Base b = p->f(); delete p;

return 0;
}

It looks like it could work to me, but is it legal? Can anyone point me to
the relevant section of the standard?

Best regards,

Matthias Hofmann





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sat Feb 28, 2004 3:33 pm    Post subject: Re: Return types of overridden functions Reply with quote



In message <c1o54t$23r$1 (AT) news1 (DOT) nefonline.de>, Matthias Hofmann
<hofmann (AT) anvil-soft (DOT) com> writes
Quote:
However, does this only apply to pointers (or references)? What if the
function returns an object, like so:

struct Base
{
virtual Base f(); // Returns an object, not a pointer.
};

struct Derived : public Base
{
virtual Derived f(); // Returns a derived object, but not a pointer either.
};

It will obviously lead to object slicing, as in the following example:

int main()
{
Base* p = new Derived;

// Calls Derived::f(), returning an object of type "Derived".
// Constructs a Base from a Derived.
Base b = p->f(); delete p;

return 0;
}

It looks like it could work to me, but is it legal? Can anyone point me to
the relevant section of the standard?

It is the absence of such a licence in the section which covers
co-variance of return types for virtual overriders that gives the clue
to the fact that it is not allowed.

And the reason it is not allowed is that calling through the virtual
mechanism and calling a function direct should have the same result and
it won't in your proposed case. Objects only have a single type,
references and pointers can have two types (the static or declared type
and the dynamic type -- the type of the object pointed to or referenced)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen C. Dewhurst
Guest





PostPosted: Sun Feb 29, 2004 2:55 am    Post subject: Re: Return types of overridden functions Reply with quote



On 27 Feb 2004 23:20:04 -0500, "Matthias Hofmann"
<hofmann (AT) anvil-soft (DOT) com> wrote:


Quote:
However, does this only apply to pointers (or references)? What if the
function returns an object, like so:

Yes, just pointers and references.

Quote:
It looks like it could work to me, but is it legal? Can anyone point me to
the relevant section of the standard?

I'm not sure that it could work, since it's much harder to play games
with offsets and addresses when one is dealing with objects rather
than pointers and references, but I haven't given it a lot of thought,
and it's possible that it's possible. Fortunately, it's not legal
(like many other things that are possible but not advisable!)

Section 10.3:5.

Steve

Steve Dewhurst
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ed Avis
Guest





PostPosted: Sun Feb 29, 2004 4:02 am    Post subject: Re: Return types of overridden functions Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> writes:

Quote:
// Calls Derived::f(), returning an object of type "Derived".
// Constructs a Base from a Derived.
Base b = p->f(); delete p;

It looks like it could work to me, but is it legal?

And the reason it is not allowed is that calling through the virtual
mechanism and calling a function direct should have the same result
and it won't in your proposed case.

Can you explain what you mean by not having the same result? It looks
as though the above code, were it allowed, would have the same effect
as

Derived *p = new Derived();
Base b = p->f();

which is allowed, slicing the return value of f() to a Base.

--
Ed Avis <ed (AT) membled (DOT) com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Feb 29, 2004 4:42 pm    Post subject: Re: Return types of overridden functions Reply with quote

In message <l1u11bdomp.fsf (AT) budvar (DOT) future-i.net>, Ed Avis
<ed (AT) membled (DOT) com> writes
Quote:
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> writes:

// Calls Derived::f(), returning an object of type "Derived".
// Constructs a Base from a Derived.
Base b = p->f(); delete p;

It looks like it could work to me, but is it legal?

And the reason it is not allowed is that calling through the virtual
mechanism and calling a function direct should have the same result
and it won't in your proposed case.

Can you explain what you mean by not having the same result? It looks
as though the above code, were it allowed, would have the same effect
as

Derived *p = new Derived();
Base b = p->f();

which is allowed, slicing the return value of f() to a Base.

Somewhere we are loosing context. I was talking about the fact that a
virtual function that returns by value bust return by the same type in
all overriders. If it did not then the type of the return would depend
on the static type of the reference or pointer used to make the call.
IOWs the derived versions would not actually be 'drop in' replacements.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.