home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Wanted: Example of Contravariance
- Message-ID: <rmartin.721098936@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <1992Nov2.111322.1@happy.colorado.edu>
- Date: Sat, 7 Nov 1992 01:15:36 GMT
- Lines: 44
-
- srheintze@happy.colorado.edu writes:
-
- >Can someone please give me a small example of contravariance I can compile and
- >run with Borland C++ v3.1 or Microsoft C++ v7?
-
- >I've seen articles in the Journal of Object Oritned Programming and in the C++
- >report but the never show me a small sample of code I can compile.
- >I just saw a new term: covariance. Is it possible to demonstrate this
- >with a short C++program?
-
- > Thanks,
- > Sieg
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-
- class B
- {
- public:
- virtual B& void f(B&);
- };
-
- class D : public B
- {
- public:
- virtual D& boid f(B&);
- };
-
- Notice that the arguments of the two 'f' functions are the same. This
- is "contravariance", since the argument does not (contra) vary with
- the class type.
-
- However, the return values are "covariant" since they do (co) vary
- with the class type.
-
- Although covariant return types have been accepted into the language
- definition, I don't think they are accepted by many of the compiler
- out ther right now. Contravariant arguments are mandatory in C++.
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-