CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    [Resloved]How do you write you're own iostream?

    Hi I'd like to make my own iostream class (for sockets).
    I'm looking http://www.cplusplus.com/reference/ but it seems to be written more for those using streams than those writing them.

    Dose anyone know where I can find an example of how do do this.

    Thanks
    Last edited by couling; January 11th, 2008 at 04:15 PM. Reason: [Resolved]

  2. #2
    Join Date
    Mar 2007
    Location
    Montreal, Quebec, Canada
    Posts
    185

    Re: How do you write you're own iostream?

    You could inherit a class from ios_base or ios, however you'll have to manually override the << operator for each type you want to be able to use.

    If you want you could probably just write your own socket class that doesn't have to come from the stream libraries. Just override the << operator yourself and you should be good (I'm assuming that's why you want the stream capabilities).

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: How do you write you're own iostream?

    I'd suggest deriving one class from istream and another from ostream, just like ifstream and ofstream are.
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: How do you write you're own iostream?

    Yes. After searching for about a day, I've finally stumbled across this:
    http://www.tacc.utexas.edu/services/...g/cre_2288.htm

    2.12.1 Choosing a base class
    ...
    Choose basic_istream <charT,Traits>, basic_ostream <charT,Traits>, or basic_iostream <charT, Traits> as a base class when deriving new stream classes, unless you have good reason not to do so.
    This allows you to make use of the existing << and >> operators for i/o/io-streams.

    Edit: I should point out that istream is a typedef of the basic_istream template, the same follows for ostream and iostream.

    This tutorial might tell me everything else I need to know. I'll mark this thread as resolved if it does.

    Thanks for the advice guys.
    Last edited by couling; January 5th, 2008 at 12:49 PM.

  5. #5
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: How do you write you're own iostream?

    Just thought I'd give a pointer to anyone doing the same.
    It seems that the iostream classes are a bit of a red herring .

    They are really only a wrapper for the streambuf class. They provide parsing functionality etc. The actual IO is controlled by the streambuf class (using the protected virtual methods: underflow(), overflow() and sync()). So I've made my own streambuf class instead.

    Once you've done this you can use a run-of-the-mill iostream object to provide you with a "custom iostream".

    Thanks for your help guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured