home *** CD-ROM | disk | FTP | other *** search
- 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
- From: davidn@csource.oz.au (david nugent)
- Newsgroups: comp.lang.c++
- Subject: overloading operator<<
- Message-ID: <6R5kwB1w165w@csource.oz.au>
- Date: 14 Nov 92 00:46:28 GMT
- Reply-To: davidn@csource.oz.au
- Organization: Unique Computing Pty Ltd, Melbourne, Australia
- Lines: 87
-
- I'm looking for some assistance, or even hints, on how to achieve
- something with iostreams. In particular, I want to overload the <<
- operator to work in a particular way.
-
- Here's an example:
-
- I've derived a class from the 'struct tm' in <time.h>, similar to
- this:
-
- #include <time.h>
-
- class ptm : public tm
- {
-
- public:
-
- typedef ostream& (ptm::*ptmf)(ostream& os); // Saves the ugly syntax
-
- // .. usual constructors, etc...
-
- short format (short _f); // Set formatting attributes
- short format (void) const; // Return formatting attributes
-
- ostream& date (ostream& os); // These print various parts
- ostream& time (ostream& os); // or combinations
- ostream& datime (ostream& os);
- ostream& year (ostream& os);
- ostream& month (ostream& os);
- ostream& day (ostream& os);
- ostream& hour (ostream& os);
- ostream& minute (ostream& os);
- ostream& second (ostream& os);
-
- friend ostream& operator<< (ostream& os, ptmf _f);
-
- private:
-
- short _tmfmt; // Time/date (default) format
-
- };
-
-
- The idea is to set particular flags and have the date format changed
- according to things such as locale, preferences or whatever..
-
- What I'd _like_ to achieve would be something like:
-
- ptm t = <initialiser>;
-
- cout << "The date is:" << t.date << '\n'
- << "The time is: " << t.time << endl;
-
- So, what I thought I'd do is to overload the << operator to accept a
- pointer to a function. I tried this:
-
- inline ostream&
- operator<< (ostream& os, ptm::ptmf _f)
- {
- _f (os);
- return os;
- }
-
- where ptmf (pointer-to-member-function) is typedef'ed as above the
- class definition.
-
- Now, gcc 2.2.2 _refuses_ to compile this, claiming that it is allowing
- illegal access outside of a class. Well, I guess that's true, but it
- is defined as a friend function after all. MSC/C++ compiles this just
- fine, but in calling the ptmf, it doesn't pass 'this', with all sorts
- of strange and unpredictable results.
-
- I tried a second solution, which involved having operator<< as a member
- function, but there seems to be no way that I can pass more than one
- argument to a bitwise operator (being the ostream reference and the
- function to call). Perhaps I just can't get the syntax right on it,
- but a search from one end of the ARM to the other didn't help either...
-
- Any helpful comments would be appreciated. I'm probably missing something
- obvious.
-
-
- david
-
-
- --
- davidn@csource.oz.au 3:632/348@fidonet 58:4100/1@intlnet 199:4242/5@rainbownet
- Unique Computing P/L / Communications+LAN Specialists / Public Access USENET
-