home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15897 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.6 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Wanted: Example of Contravariance
  5. Message-ID: <rmartin.721098936@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <1992Nov2.111322.1@happy.colorado.edu>
  9. Date: Sat, 7 Nov 1992 01:15:36 GMT
  10. Lines: 44
  11.  
  12. srheintze@happy.colorado.edu writes:
  13.  
  14. >Can someone please give me a small example of contravariance I can compile and 
  15. >run with Borland C++ v3.1 or Microsoft C++ v7?
  16.  
  17. >I've seen articles in the Journal of Object Oritned Programming and in the C++
  18. >report but the never show me a small sample of code I can compile.
  19. >I just saw a new term: covariance.  Is it possible to demonstrate this 
  20. >with a short C++program?
  21.  
  22. >            Thanks,
  23. >                    Sieg
  24.  
  25. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  26.  
  27. class B
  28. {
  29.   public:
  30.     virtual B& void f(B&);
  31. };
  32.  
  33. class D : public B
  34. {
  35.   public:
  36.     virtual D& boid f(B&);
  37. };
  38.  
  39. Notice that the arguments of the two 'f' functions are the same.  This
  40. is "contravariance", since the argument does not (contra) vary with
  41. the class type.  
  42.  
  43. However, the return values are "covariant" since they do (co) vary
  44. with the class type.
  45.  
  46. Although covariant return types have been accepted into the language
  47. definition, I don't think they are accepted by many of the compiler
  48. out ther right now.  Contravariant arguments are mandatory in C++.
  49.  
  50.  
  51. --
  52. Robert Martin                        Training courses offered in:
  53. R. C. M. Consulting                       Object Oriented Analysis
  54. 2080 Cranbrook Rd.                        Object Oriented Design
  55. Green Oaks, Il 60048 (708) 918-1004       C++
  56.