Thread View: gnu.g++.help
2 messages
2 total messages
Started by Albert Golembiowski
Wed, 25 Oct 1995 00:31
covariance anyone?
Author: Albert Golembiowski
Date: Wed, 25 Oct 1995 00:31
Date: Wed, 25 Oct 1995 00:31
81 lines
3353 bytes
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 <-
--
Re: covariance anyone?
Author: Jason Merrill
Date: Wed, 25 Oct 1995 06:34
Date: Wed, 25 Oct 1995 06:34
37 lines
1877 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads