home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!demos!news-server
- From: alex@erma.msk.su (alex)
- Subject: Re: defining cast operators outside classes
- References: <TMB.92Sep7162324@arolla.idiap.ch>
- Date: Tue, 8 Sep 1992 11:18:45 GMT
- Reply-To: alex@erma.msk.su
- Organization: JV ERMA International
- Sender: news-server@kremvax.hq.demos.su
- Message-ID: <AALm8hgCn3@erma.msk.su>
- Lines: 32
-
- 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++?
- >
- > Thomas.
- >
-
- Conversion operators were invented to allow implicit typecasting
- for user defined data types (i.e. classes). Since it is impossible to cast
- "nothing", conversion function should have an argument - the object that is
- to be typecasted. Making conversion operators to be member functions forces
- this requirement to be always met. Otherwise one could write something like:
-
- operator MyMatrix() {
- ...
- }
-
- ------------------------------------------------------------------------
- Alexander Pavlov ERMA International, Moscow, Russia
-
-
-