🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

covariance anyone?

#799
From: Albert Golembiowski
Date: Wed, 25 Oct 1995 00:31
81 lines
3353 bytes
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?

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

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

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Albert Golembiowski      albert@inrs-telecom.uquebec.ca         (514) 762-8655
INRS Telecommunications, 16 Place du Commerce, Verdun, Quebec, Canada, H3E 1H6
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

P.S. To illustrate, this working example shows this is indeed the case,
     as indicated with `<-' for two runs: 
         1-st with //(2) commented off
         2-nd with the //(2) in place.

------------------------ cut ---------------------------
#include <iostream.h>
class B 
{
protected:
  int sz;
public:
  B (int Sz=0): sz(Sz) {}
  virtual ~B() {}
  virtual int Size () const {  cout << " size B:" << sz << ' '; return sz; }
  virtual B& _ReSize (int Sz) { sz=Sz; cout << ", resize B:" << sz << ' '; return *this; }
};
class D: public B 
{
public:
  D(int Sz=0): B(Sz) {}
  D& operator = (const D& rhs) { sz = rhs.sz; return *this; }
  int Size () const {  cout << " size D:" << sz << ' '; return sz; }
//  virtual D& _ReSize (int Sz) { sz = Sz; cout << " resize D:" << sz << ' '; return *this; }  //(2)
};

void print_B (B& A) { A.Size(); A._ReSize(5);  A._ReSize(5).Size(); cout << endl;}
void print_D (D& A) { A.Size(); A._ReSize(8);  A.Size(); cout << endl;}

int main () {
  B b(4);  D d(4);
  print_B(b);
  print_B(d);
  print_D(d);
  d._ReSize(20).Size();
  cout << endl;
}
------------------------ cut ---------------------------
 g++-2.7.0 -g -I/usr/local/lib/g++-include-2.7.0 -ansi test.cc -o test
------------------------ run 1 ---------------------------
size B:4 , resize B:5 , resize B:5  size B:5
 size D:4 , resize B:5 , resize B:5  size D:5      <-
 size D:5 , resize B:8  size D:8
, resize B:20  size D:20                           <-
------------------------- run 2 ----------------------
 size B:4 , resize B:5 , resize B:5  size B:5
 size D:4  resize D:5  resize D:5  size D:5        <-
 size D:5  resize D:8  size D:8
 resize D:20  size D:20                            <-

-- 
Technical Details
Message-ID: <9510250031.AA29088@brie>
Path: arc1-ca.pugleaf.net!inrs-telecom.uquebec.ca!albert

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!inrs-telecom.uquebec.ca!albert
From: albert@inrs-telecom.uquebec.ca (Albert Golembiowski)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: covariance anyone?
Date: 24 Oct 1995 20:31:52 -0400
Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu
Sender: daemon@cis.ohio-state.edu
Distribution: world
Message-ID: <9510250031.AA29088@brie>