home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!menudo.uh.edu!sugar!daniels
- From: daniels@NeoSoft.com (Brad Daniels)
- Subject: Adding default conversion to char?
- Organization: NeoSoft Communications Services -- (713) 684-5900
- Date: Thu, 7 Jan 1993 18:52:33 GMT
- Message-ID: <C0Hz3M.4s0@NeoSoft.com>
- Lines: 32
-
- I am writing a shared compound string class as a self-imposed exercise
- to learn C++. One of the things I want to do is allow people to assign
- to a character within a shared string by making the assignment generate
- a copy of the string with the character changed.
-
- To achieve this, I created a "shared character" class, which is just a
- character with a pointer to the shared string it comes from. I then
- overload the [] operator on the shared string to return a shared character,
- and overload the = operator on shared characters to handle string cloning.
- All this is fairly straightforward.
-
- I would like, however, to allow shared characters to be treated like characters
- under every other circumstance. I.e., I want to add a rule for casting a
- shared character to type int or char, which simply yields the value of the
- character field in the shared character object. I can't figure out a way
- to do this,though, since char isn't really a class. The only way I can
- think to achieve what I want is to overload every operator which might
- read a character value so that it knows what to do with a shared character.
- Obviously, that would be a lot more work, and won't handle the case where
- a function call requires an argument of type char.
-
- Is there a way to achieve what I want? Since this is mostly a learning
- exercise, I'm not desperate for a work-around, but I'd be interested in
- hearing alternate ways to implement what I want to do if the approach I'm
- trying isn't feasible.
-
-
- The newsreader on this machine seems to be having trouble with this news-
- group, so I'd appreciate if you could e-mail replies as well as posting.
-
- Thanks,
- - Brad
-