home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13406 < prev    next >
Encoding:
Text File  |  1992-09-08  |  1.5 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!demos!news-server
  3. From:  alex@erma.msk.su (alex)
  4. Subject: Re: defining cast operators outside classes
  5. References: <TMB.92Sep7162324@arolla.idiap.ch>
  6. Date: Tue, 8 Sep 1992 11:18:45 GMT
  7. Reply-To: alex@erma.msk.su
  8. Organization: JV ERMA International
  9. Sender: news-server@kremvax.hq.demos.su
  10. Message-ID: <AALm8hgCn3@erma.msk.su>
  11. Lines: 32
  12.  
  13. tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  14. >   Apparently, it is impossible to define a conversion operator for a
  15. >   class without making the conversion operator a member function.
  16. >
  17. >   It would be very useful to be able to define something like the
  18. >   following without having to modify either class MyMatrix or class
  19. >   TheirMatrix:
  20. >
  21. >           operator MyMatrix(TheirMatrix &m) {
  22. >                   ...
  23. >           }
  24. >
  25. >   Why was this seemingly arbitrary restriction made? Can it be
  26. >   eliminated in the next version of C++?
  27. >
  28. >                                           Thomas.
  29. >
  30.  
  31.         Conversion operators were invented to allow implicit typecasting
  32. for user defined data types (i.e. classes). Since it is impossible to cast
  33. "nothing", conversion function should have an argument - the object that is
  34. to be typecasted. Making conversion operators to be member functions forces
  35. this requirement to be always met. Otherwise one could write something like:
  36.  
  37.         operator MyMatrix() {
  38.                 ...
  39.         }
  40.  
  41. ------------------------------------------------------------------------
  42. Alexander Pavlov                      ERMA International, Moscow, Russia
  43.  
  44.  
  45.