Thread: Input class

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    Currently the operators throws if there is an error (only input, though). I wonder if it's a good idea to make them return a bool? It would make it impossible to chain them, but one wouldn't have to try put try/catch blocks to use them.
    IIRC, stream insertors and extractors are already convertible to a bool type for the purpose you're thinking of, and that doesn't mitigate the possibility for exceptions. But I have to say I don't think that insertors or extractors are exceptional operations... they usually always attempt to do their work, and return a bad stream if something happened. Unless you're one of those people who opt to throw an exception instead of say program by cases...

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    Code:
    #ifndef TCHAR
    	#ifdef UNICODE
    		#define TCHAR wchar_t
    	#else
    		#define TCHAR char
    	#endif
    #else
    	#define TCHAR char
    #endif
    Dangerous. windows.h handle the TCHAR issue with a typedef, not a macro, so your #ifdef won't trigger. (windows.h defines _TCHAR_DEFINED, but I wouldn't rely on it.)
    Since a typedef would be scoped anyway, just make it a typedef.
    Hmmm.
    OK, I changed that a bit.

    Now the only problem would be the operators...
    Should they cast? Should they not?
    Perhaps they should complement the member functions (both throw & non-throw).
    And I wouldn't have the slightest clue how to return a "bad stream" in case they fail.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> And I wouldn't have the slightest clue how to return a "bad stream" in case they fail.

    You shouldn't need to "do" anything. Your run-of-the-mill insertor/extractor ...
    Code:
    std::ostream & operator<< (std::ostream & out, const object & o )
    {
      out << o.foo();
      out << o.bar();
      return out;
    }
    If foo and bar return plain old data then there is no problem, the state of the stream is determined by the success or failure of the conversion. If foo and bar do not return plain old data then the state of the stream is determined by the result of operator << for the object.

    It would be better if you understood that by default streams do not throw.

    If you want a specific state to be exceptional you usually call exception( mask ); beforehand.
    Last edited by whiteflags; 10-19-2008 at 02:59 PM.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that I don't return a stream, but rather the "input object." I'd have to define some "bad" state implementation I guess.
    Along with a throw-version and a non-throw version of the operator equivalent member functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. problem of input in member function of class
    By Roy01 in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2006, 10:27 AM
  4. Input Class
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 09:21 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM