home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18811 < prev    next >
Encoding:
Text File  |  1993-01-07  |  1.9 KB  |  42 lines

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