home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11596 < prev    next >
Encoding:
Text File  |  1992-07-27  |  2.6 KB  |  61 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ascent!eb
  3. From: eb@ascent.com (Ed Barton)
  4. Subject: Re: Conversion operators and typedef names
  5. In-reply-to: krc@wam.umd.edu's message of Tue, 21 Jul 1992 16:09:17 GMT
  6. Message-ID: <EB.92Jul27085934@ascent.ascent.com>
  7. Date: 27 Jul 92 08:59:34
  8. References: <1992Jul21.132931.8728@ohm.york.ac.uk> <1992Jul21.160917.27353@wam.umd.edu>
  9. Organization: Ascent Technology, Inc., Cambridge Massachusetts
  10. Lines: 49
  11.  
  12.  
  13. In article <1992Jul21.160917.27353@wam.umd.edu> krc@wam.umd.edu (Kevin R. Coombes) writes:
  14.  
  15.    class MyTime {
  16.      ...
  17.      operator time_t(void);   // Does this make you queasy?
  18.      ...
  19.    };
  20.  
  21. Technically, this works.  However, when I have tried it, I have found
  22. that it is usually a bad idea in practice.  Remember that a typedef
  23. does not define a new type, so on a particular machine, this is just
  24. as if you had defined operator unsigned long() or operator long() or
  25. whatever, depending on how time_t is actually defined.  That means you
  26. can't have an operator time_t() and an operator other_t() at the same
  27. time, if other_t is a typedef name that happens to come out the same
  28. as time_t on your machine.  Also, the compiler will insert a call to
  29. operator time_t() if you call a function f(unsigned long) on one of
  30. your MyTime objects.  time_t is just the same as unsigned long (or
  31. whatever) for purposes of overloading, so you will get a conversion
  32. even when the unsigned long that is expected is not really a time_t.
  33.  
  34. (In my case, I had two different time representations that were both
  35. internally represented as the same basic type.)
  36.  
  37.    The alternative, assuming you really do want to be able to convert your
  38.    time objects into time_t's, is to supply a method like
  39.  
  40.      time_t MyTime::getValue(void);
  41.  
  42. At least when typedefs are involved, this is a better idea.  For
  43. operator struct tm(), the answer might be different, if you can figure
  44. out whether it should do a GMT or local time conversion.
  45.  
  46. The implicit conversions provided by constructors lead to similar
  47. problems, if you have one-argument constructors that take time_t or
  48. similar arguments.  Depending on the actual typedefs on your machine,
  49. you might be surprised to have a plain integer in the code converted
  50. into a MyTime when there is a constructor taking a time_t.
  51.  
  52. Any kind of overloading on typedef names can lead to portability
  53. problems, if the typedef names are likely to be defined differently on
  54. different machines.  For instance, if you have both operator int() and
  55. operator time_t(), and time_t happens to be defined as int on some
  56. machines, then you will have a clash on those machines but not others.
  57.  
  58.  
  59.  
  60.  
  61.