Re: Covariant returns

Zalman Stern <>
28 Nov 1999 01:37:11 -0500

          From comp.compilers

Related articles
Covariant returns (Lakshman KNVSM) (1999-11-23)
Re: Covariant returns (Zalman Stern) (1999-11-28)
| List of all articles for this month |

From: Zalman Stern <>
Newsgroups: comp.compilers
Date: 28 Nov 1999 01:37:11 -0500
Organization: MindSpring Enterprises
References: 99-11-131
Keywords: C++, code

Lakshman KNVSM <> wrote:
: Hi,
: How are covariant returns implemented in C++? I mean, how is the pointer
: adjustment required done in case of Multiple Inheritance?

: [This came up a couple of days ago. Either with function wrappers (often
: incorrectly called thunks) or with an adjustment field in the vtbl. -John]

I'm not sure I understand John's comment. I wouldn't expect function
wrappers to be generated for covariant return types.

        class base {
virtual base *f();

        class derived : public base {
#if COVARIANT_RETURNS // For illustrative purposes only...
virtual derived *f();
virtual base *f();

The programmer must provide two definitions of f(), base::f() and
derived::f() with appropriate return types. The compiler will not
automatically generate a derived::f(). In the case of multiple
inheritance and covariant return types, I would expect the compiler to
generate code such that member functions always returns a pointer to
the topmost base class that defines the member function (as they
always have right?) and then when the member function is called,
generate code to apply any needed offset right there. Adding covariant
return types to the language allows this to happen in a safe static
typed way without writing explicit casts.

So given the above class declarations, one writes:
derived *foo = ...;
derived *bar = foo->f();
derived *bar = static_cast<derived *>(foo->f());

I'd expect the same code to be generated whether COVARIANT_RETURNS is true
or not. But perhaps I'm missing something.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.