home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16254 < prev    next >
Encoding:
Text File  |  1992-11-14  |  2.9 KB  |  98 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!att-out!rutgers!cs.utexas.edu!sdd.hp.com!spool.mu.edu!sgiblab!munnari.oz.au!jabaru.cec.edu.au!csource!davidn
  2. From: davidn@csource.oz.au (david nugent)
  3. Newsgroups: comp.lang.c++
  4. Subject: overloading operator<<
  5. Message-ID: <6R5kwB1w165w@csource.oz.au>
  6. Date: 14 Nov 92 00:46:28 GMT
  7. Reply-To: davidn@csource.oz.au
  8. Organization: Unique Computing Pty Ltd, Melbourne, Australia
  9. Lines: 87
  10.  
  11. I'm looking for some assistance, or even hints, on how to achieve
  12. something with iostreams. In particular, I want to overload the <<
  13. operator to work in a particular way.
  14.  
  15. Here's an example:
  16.  
  17. I've derived a class from the 'struct tm' in <time.h>, similar to
  18. this:
  19.  
  20.   #include <time.h>
  21.  
  22.   class ptm : public tm
  23.   {
  24.  
  25.   public:
  26.  
  27.     typedef ostream& (ptm::*ptmf)(ostream& os);  // Saves the ugly syntax
  28.  
  29.     // .. usual constructors, etc...
  30.  
  31.     short format (short _f);         // Set formatting attributes
  32.     short format (void) const;         // Return formatting attributes
  33.  
  34.     ostream& date (ostream& os);       // These print various parts
  35.     ostream& time (ostream& os);       // or combinations
  36.     ostream& datime (ostream& os);
  37.     ostream& year (ostream& os);
  38.     ostream& month (ostream& os);
  39.     ostream& day (ostream& os);
  40.     ostream& hour (ostream& os);
  41.     ostream& minute (ostream& os);
  42.     ostream& second (ostream& os);
  43.  
  44.     friend ostream& operator<< (ostream& os, ptmf _f);
  45.  
  46.   private:
  47.  
  48.      short _tmfmt;             // Time/date (default) format
  49.  
  50.   };
  51.  
  52.  
  53. The idea is to set particular flags and have the date format changed
  54. according to things such as locale, preferences or whatever..
  55.  
  56. What I'd _like_ to achieve would be something like:
  57.  
  58.   ptm t = <initialiser>;
  59.  
  60.   cout << "The date is:" << t.date << '\n'
  61.        << "The time is: " << t.time << endl;
  62.  
  63. So, what I thought I'd do is to overload the << operator to accept a
  64. pointer to a function.    I tried this:
  65.  
  66.   inline ostream&
  67.   operator<< (ostream& os, ptm::ptmf _f)
  68.   {
  69.     _f (os);
  70.     return os;
  71.   }
  72.  
  73. where ptmf (pointer-to-member-function) is typedef'ed as above the
  74. class definition.
  75.  
  76. Now, gcc 2.2.2 _refuses_ to compile this, claiming that it is allowing
  77. illegal access outside of a class.  Well, I guess that's true, but it
  78. is defined as a friend function after all.  MSC/C++ compiles this just
  79. fine, but in calling the ptmf, it doesn't pass 'this', with all sorts
  80. of strange and unpredictable results.
  81.  
  82. I tried a second solution, which involved having operator<< as a member
  83. function, but there seems to be no way that I can pass more than one
  84. argument to a bitwise operator (being the ostream reference and the
  85. function to call). Perhaps I just can't get the syntax right on it,
  86. but a search from one end of the ARM to the other didn't help either...
  87.  
  88. Any helpful comments would be appreciated.  I'm probably missing something
  89. obvious.
  90.  
  91.  
  92. david
  93.  
  94.  
  95. --
  96. davidn@csource.oz.au 3:632/348@fidonet 58:4100/1@intlnet 199:4242/5@rainbownet
  97. Unique Computing P/L / Communications+LAN Specialists / Public Access USENET
  98.