home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!wupost!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
- From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Subject: Re: defining cast operators outside classes
- Message-ID: <1992Sep7.154125.28472@murdoch.acc.Virginia.EDU>
- Sender: usenet@murdoch.acc.Virginia.EDU
- Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Organization: University of Virginia
- References: <TMB.92Sep7162324@arolla.idiap.ch>
- Date: Mon, 7 Sep 1992 15:41:25 GMT
- Lines: 39
-
- tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
- : Apparently, it is impossible to define a conversion operator for a
- : class without making the conversion operator a member function.
- :
- : It would be very useful to be able to define something like the
- : following without having to modify either class MyMatrix or class
- : TheirMatrix:
- :
- : operator MyMatrix(TheirMatrix &m) {
- : ...
- : }
- :
- : Why was this seemingly arbitrary restriction made? Can it be
- : eliminated in the next version of C++?
-
- You can achieve what you want by providing a constructor in your
- MyMatrix class that takes a reference to TheirMatrix as its
- argument. For example,
-
- class MyMatrix {
- ...
- public:
- MyMatrix(const TheirMatrix& m) {
- //convert TheirMatrix into MyMatrix
- }
- ...
- };
-
- void f(const MyMatrix& m) {...}
-
- main()
- {
- TheirMatrix tm(10,10);
- MyMatrix mm = tm;
- ...
- f(tm);
- }
-
- -Sekar
-