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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
  3. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  4. Subject: Re: defining cast operators outside classes
  5. Message-ID: <1992Sep7.154125.28472@murdoch.acc.Virginia.EDU>
  6. Sender: usenet@murdoch.acc.Virginia.EDU
  7. Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  8. Organization: University of Virginia
  9. References:  <TMB.92Sep7162324@arolla.idiap.ch>
  10. Date: Mon, 7 Sep 1992 15:41:25 GMT
  11. Lines: 39
  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. : It would be very useful to be able to define something like the
  17. : following without having to modify either class MyMatrix or class
  18. : TheirMatrix:
  19. :     operator MyMatrix(TheirMatrix &m) {
  20. :         ...
  21. :     }
  22. : Why was this seemingly arbitrary restriction made? Can it be
  23. : eliminated in the next version of C++?
  24.  
  25. You can achieve what you want by providing a constructor in your
  26. MyMatrix class that takes a reference to TheirMatrix as its
  27. argument. For example,
  28.  
  29. class MyMatrix {
  30.     ...
  31. public:
  32.     MyMatrix(const TheirMatrix& m) {
  33.         //convert TheirMatrix into MyMatrix
  34.     }
  35.     ...
  36. };
  37.  
  38. void f(const MyMatrix& m) {...}
  39.  
  40. main()
  41. {
  42.     TheirMatrix    tm(10,10);
  43.     MyMatrix    mm = tm;
  44.     ...
  45.     f(tm);
  46. }
  47.  
  48. -Sekar
  49.