Input class

This is a discussion on Input class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Currently the operators throws if there is an error (only input, though). I wonder if it's ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    >> 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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 2 of 2 FirstFirst 12
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, 01:20 PM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. problem of input in member function of class
    By Roy01 in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2006, 09: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, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21