🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #800

Re: covariance anyone?

#800
From: Jason Merrill
Date: Wed, 25 Oct 1995 06:34
37 lines
1877 bytes
>>>>> Albert Golembiowski <albert@inrs-telecom.uquebec.ca> writes:

> Dear Expert,
> Just 'd like to hear a confirmation that the g++-2.7.0 
> implementation of covariance is standard-compliant, stable, and it means:

>  [1] a virtual publicly inherited method
>        which returns a reference to a base object `b' of type `B' 

>         `virtual B& _Method() { /* do something */ return *this; }'
     
>       returns a reference to the derived object `d' of type `D' if called
>       from it, or through a base pointer, i.e. saying the following is
>       redundant
 
>          `virtual D& _Method() { B::_Method(); return *this; }' // 2

>       and only clutters the interface (and sweats the prerocessor) if
>       that all that `D& D::_Method()' is meant to do?

No.  The second method must be written if you want covariance.

>  [2] Is the statement //2// ever needed?

It is necessary for covariance.

>  [3] If I need a reference to B object returned from `d._Method()'
>      instead, do I always need to downcast?

You need to upcast, or you can rely on the implicit conversion if that will
work.

Your example does not test your hypothesis because Size is a virtual
function, so the D method is called even when the object parameter has a
static type of B.

Jason
Technical Details
Message-ID: <u9ka5uf4vl.fsf@yorick.cygnus.com>
Path: arc1-ca.pugleaf.net!cygnus.com!usenet
References: <9510250031.AA29088@brie>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!cygnus.com!usenet
From: jason@cygnus.com (Jason Merrill)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re: covariance anyone?
Date: 24 Oct 1995 23:34:54 -0700
Organization: Cygnus Support, Mountain View, CA
Sender: jason@yorick.cygnus.com
Message-ID: <u9ka5uf4vl.fsf@yorick.cygnus.com>
References: <9510250031.AA29088@brie>
NNTP-Posting-Host: yorick.cygnus.com
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
In-reply-to: albert@inrs-telecom.uquebec.ca's message of 24 Oct 1995 20:31:52
 -0400
X-Newsreader: Gnus v5.0