C++
  Home arrow C++ arrow Page 3 - Custom Stream Manipulation in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Custom Stream Manipulation in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-05

    Table of Contents:
  • Custom Stream Manipulation in C++
  • Manipulators with No Argument
  • Solution for Multiple Arguments
  • Templates on the Fly

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Custom Stream Manipulation in C++ - Solution for Multiple Arguments


    (Page 3 of 4 )

    This is all fine; I applied three manipulators with the creation of a new one, or transformed the reset of the float flags into a manipulator -- but even with this, I had to call the setprecision explicitly to set the precision. In a perfect scenario, I could do this with just the scientify(3) call.

    To figure out how we can do this, let us investigate how to make this the developing team inside the STL library. How can the example on the previous page work? It works because inside the iostream library there is, defined for the insertion operator, the following:


    basic_ostream& operator<<

    (basic_ostream& (__clrcall *_Pfn)( basic_ostream&));


    //where-> event_callback _Pfn; // pointer to event handler


    Inside this, the function just takes a basic_ostream reference and returns the same reference; it simply takes a function and passes it further. Nevertheless, if you plan to pass some parameters inside the function as well, there is a problem. How can you pass them toward the function so that you will know the stream has them also? The obvious answer is to include the stream into its parameters.

    Now if I start doing so I end up with something like this:


    cin << streamify(3, cin);


    This is a redundant solution and not elegant under any circumstances; we can find a better solution. The main power of the manipulators is to add it with the << operator to the function. We must not break that convention!

    The solution is to trick the compiler. In addition, for this we need to use all of the overloading capabilities of C++. First, create a class that will do the work. For this we declare a local variable holding the input number and an overload for the () operator. The basic constructor will naturally exist inside, where we need to initialize the data.


    class tempForScience

    {

    tempForScience (int n) { m_v = n };

    void operator() (ostream& stream) const

    {stream << scientify << setprecision(m_v); }; // here we can // whatever we want on the stream, for instance apply more // manipulators on it

    int m_v;

    }


    This class will do the work for us, but we must initialize a type like this during the stream insertion, so we write a function that will just return a creation from this:


    tempForScience setScientify(int n)

    {

    return tempForScience(n);

    }


    Now we called the function we want, but the problem that we still have is that the insertion operator will not know what to do with the tempForScience() object we just returned. We just need to take another step to resolve this issue.


    ostream& operator<<(ostream& str, const tempForScience & tFS) {

    tFS (str); // Pass the stream to the tfs object

    // to do iterate back and accomplish all

    return(str); // return the stream so we can insert // objects further in the stream

    }


    Insert all we learned for now after the scientify manipulator in the earlier code and change the call lines for the cout to the following:


    cout << setScientify(3) << i << endl;

    cout << resetFloat << i << endl;


    The result is what we were aiming for, hurray!


    +3.165e+000

    +3.17

    Press any key to continue . . .



    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek