CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Output manipulation and overloading <<

  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Question Output manipulation and overloading <<

    Hi all,

    Let's say I want to output many long strings of text to the output console
    E.g.
    Code:
    cout << "This is long string of text etc etc etc etc etc";
    cout << "This is another long string of text etc etc etc etc etc";
    Now to make it look neater on the output console, I have a word wrapping function that takes any string as an argument. It splits the text into a new line every 80 characters by taking the last instance of a 'space' within the 80 characters and changing that to a line-break '\n'. However to call this function I have to have:
    Code:
    cout << wordWrap("This is long string of text etc etc etc etc etc");
    cout << wordWrap("This is another long string of text etc etc etc etc etc");
    Making all these calls to wordWrap gets messy after many cout's throughout my code. So could I overload the operator (<<) so that my program automatically word wraps every instance of cout << "text". I was thinking of something this:
    Code:
    ostream& operator<<(ostream& os, const string& str) {
    	os << wordWrap(str);
    	return os;
    }
    I tried that, but as far as I know I have to use 'const' and as the argument is a constant, it won't let me perform wordWrap(str). Am I on the right track?

    I don't expect an entire solution, but any advice such as whether or not I'm going the right direction with this would be greatly appreciated.

    Thanks,
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Output manipulation and overloading <<

    Unfortunately, I believe that it is a member operator<< that would be matched by overload resolution, so such an overload would not work in the first place since it is a different operator<< that would be matched when you print a string literal in that way.

    Quote Originally Posted by shaakka
    I tried that, but as far as I know I have to use 'const' and as the argument is a constant, it won't let me perform wordWrap(str). Am I on the right track?
    You would already have that problem when wordwrapping a string literal. Perhaps you should show us the prototype of the wordWrap function... I expect that you are making a copy of the string anyway, so that should not be a problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2011
    Posts
    3

    Re: Output manipulation and overloading <<

    Hmm ok, and thanks for your quick reply

    Just the prototype?
    Code:
    string wordWrap(string strToWrap);

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Output manipulation and overloading <<

    Quote Originally Posted by shaakka
    Just the prototype?
    Yes, and that confirms what I suspected: you are using pass by value, so within wordWrap you are dealing with a copy of the string. No problem... other than the problem with overload resolution that I mentioned.

    My instinct here is to say: don't bother, just type.

    If you insist, one option is to write an output stream manipulator so you can write stuff like:
    Code:
    cout << wordwrap;
    cout << "This is long string of text etc etc etc etc etc";
    cout << "This is another long string of text etc etc etc etc etc";
    cout << nowordwrap;
    A quick search brings up this discussion: C++ custom stream manipulator that changes next item on stream. It is concerned with manipulating numeric output, but something similiar should apply for manipulating string output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Sep 2011
    Posts
    3

    Re: Output manipulation and overloading <<

    Ok, and thanks again

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Output manipulation and overloading <<

    Quote Originally Posted by laserlight View Post
    A quick search brings up this discussion: C++ custom stream manipulator that changes next item on stream. It is concerned with manipulating numeric output, but something similiar should apply for manipulating string output.
    That is some VERY interesting read. I had tried custom manipulators before, but it was difficult, since I did not know about xalloc, it was a bit difficult

    I do have a question for you though (which should apply to the OP question): In the link, the answer imbues the locale with his own num_put, to "plug" into the output stream behavior. This works because the author is redefining the way a number is transformed to a string. In essence, he has not plugged into the stream, but the "translation engine". He merely stored the "activation bit" into the stream.

    You say "something similar" should apply to strings. However, I was unable to find anything inside locale.

    If I were to ask you:
    I need a joke manipulator, that changes the charactor 'e' to 'a' in a such a way that:
    Code:
    std::cout << my_joke_manipulator;
    std::cout << "hello" << " " << "world!"
    gives
    Code:
    hallo world!
    If you can answer this, I'm sure we can find something for the OP.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Output manipulation and overloading <<

    I would probably do this with a custom streambuf "shim", so the buffer can be manipulated just prior to being synced with the original streambuf.

    Here's an example of a streambuf "shim" that just forwards data into 2 other stream buffers: http://www.codeguru.com/forum/showpo...0&postcount=18

    gg

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Output manipulation and overloading <<

    Quote Originally Posted by monarch_dodra
    You say "something similar" should apply to strings. However, I was unable to find anything inside locale.
    Unfortunately, neither have I been able to arrive at a solution.

    The best attempt I have seen after searching the Web is one where a wrapper for the output stream is used. The construction of this wrapper is disguised as a manipulator of the output stream, though in actual fact the stream is substituted for the wrapper. Hence, the string output is processed by the wrapper, and thereby can be modified and only then sent to the output stream. Of course, when the lifetime of the wrapper object is over, the manipulator effect ends, so this does not achieve the effect that we have in mind.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Output manipulation and overloading <<

    Quote Originally Posted by Codeplug View Post
    I would probably do this with a custom streambuf "shim", so the buffer can be manipulated just prior to being synced with the original streambuf.

    Here's an example of a streambuf "shim" that just forwards data into 2 other stream buffers: http://www.codeguru.com/forum/showpo...0&postcount=18

    gg
    I had told myself that streambuffers couldn't work, since I wanted a manipulator, but come to think about, no reason my manipulator can't introduce or remove a buffer...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)