Converting an ifstream to an istream

This is a discussion on Converting an ifstream to an istream within the C++ Programming forums, part of the General Programming Boards category; Boy, am I beginning to hate C++, and here's one of the reasons why: Me (to compiler): Code: #include <fstream> ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    25

    Converting an ifstream to an istream

    Boy, am I beginning to hate C++, and here's one of the reasons why:

    Me (to compiler):
    Code:
    #include <fstream>
    
    int important_function(std::istream ){
    	return 0;
    }
    
    int main() {
    	std::ifstream f("dummy_file");
    	important_function(f);
    	return 0;
    }
    Compiler (to me): 'important_function' : cannot convert parameter 1 from 'std::ifstream' to 'std::istream'

    Me (to compiler): How about this?
    Code:
    #include <fstream>
    
    int important_function(std::istream ){
    	return 0;
    }
    
    int main() {
    	std::ifstream f("dummy_file");
    	important_function(*(reinterpret_cast<istream *>(f)));
    	return 0;
    }
    Compiler (to me): 'important_function' : cannot convert parameter 1 from 'std::istream' to 'std::istream'

    I ended up using a reference, which I don't need. I only need the value. Why can't it just copy the damn structure? Is there any way to make it to?

    Thank you for your time :-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    As far as I can tell from the C++ standard, the copy constructor and assignment operator for std::istream is disabled (i.e. private).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    25
    Thanks. So there's no way to copy one (save for modifying the header)?
    I'll use memcpy(), just to spite it.
    As I said, I hate C++.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    So there's no way to copy one
    Why do you need to copy? The use of references here is more efficient anyway.

    I'll use memcpy(), just to spite it.
    Dont, a std::istream is not a POD.

    As I said, I hate C++.
    Then dont use it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by manannan
    Thanks. So there's no way to copy one (save for modifying the header)?
    I'll use memcpy(), just to spite it.
    As I said, I hate C++.
    Why work against it? A const reference will give you the same thing minus the overhead of copying. Further, using memcpy just sounds like a bad idea.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    25
    Quote Originally Posted by laserlight
    Why do you need to copy? The use of references here is more efficient anyway.
    I don't know... it's just that if my program uses some data that I can't tamper with, I feel uncomfortable :-)
    Quote Originally Posted by laserlight
    Then dont use it.
    Perhaps I was too rash in saying this. C++ implements C as a subset, and thus is no worse than C. Perhaps someday I'll grok all these fancy new features, too.
    Last edited by manannan; 08-30-2005 at 11:57 AM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Using a const reference is a much better idea.

    a) You are using polymorphism -- the reason why ifstream derives from istream in the first place.
    b) It is type safe.
    c) You are in no danger of modifying something you don't want to.
    d) It is more efficient.
    e) Reinterpret cast should only be used if you know what you are doing, and if there is no other way (read, "very rarely").

    No Todo, we're not on the C Board anymore.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So there's no way to copy one (save for modifying the header)?
    There is, but you don't want to do it. A much easier solution would be to just pass references when working with stream objects. That's how they're designed, and that's how you should use them.
    I'll use memcpy(), just to spite it.
    Good luck. Doing a byte by byte copy of a non-POD type is dangerous to the max.
    it's just that if my program uses some data that I can't tamper with, I feel uncomfortable :-)
    C does this in the standard libraries. Every used strtok? You're not allowed to fiddle with the contents of a FILE *, so why should you think it's okay with iostreams?
    Just because I don't care doesn't mean I don't understand.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    A const reference will give you the same thing minus the overhead of copying.
    Using a const reference is a much better idea.
    hmm... I had the impression that using a std::istream (or std::ostream, for that matter) means changing it. Under what circumstances would we pass a std::istream as a const reference?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    That's true; as far as I can tell, there aren't any const functions for istreams.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    As a general rule, if you're passing an istream (or an ifstream) as an argument, you want to read data from it. Hence one typically passes the stream by reference, so all attempts to read come from the same actual stream (eg the same file, opened with the same file handle, etc).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2007, 06:11 PM
  2. ifstream and istream issue
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 07-03-2005, 07:43 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. converting an ifstream to a file
    By WebmasterMattD in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2003, 08:57 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM

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