Covariant returns?

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Tue Mar 7 01:08:00 GMT 2000


> The This() methods were specified to be virtual.  Meaning if you have a
> pointer of a base type to the dereived object and you invoke any virtual
> methods that are defined in the derived class the actual method invoked
> will be the one for the derived class.
> 
> That is why invoking    a->This(), b->This(), c->This()
> will all invoke         C::This()

Sure, I understand that part. However, 5.2.2, [expr.call]/4 says

# The value of a function call is the value returned by the called
# function except in a virtual function call if the return type of the
# final overrider is different from the return type of the statically
# chosen function, the value returned from the final overrider is
# converted to the return type of the statically chosen function.

So even though C::This is called, the return value must be converted
to B*, when calling the virtual function for a B*.

Martin


More information about the Gcc mailing list