Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function returning "stream" ?

Reply
Thread Tools

Function returning "stream" ?

 
 
StephQ
Guest
Posts: n/a
 
      03-06-2007
At the moment I have a void member function (of a given class) that
takes as input an obj of class type ofstream and write some debug
information to it using the << operator.
I would like to modify the code so that the function returns a
"stream" that can be "sent" to cout or to a file using <<, as this
would give me more flexibility.
I used " " because I'm not sure I'm not using the correct terms
here.
Is there an easy way to accomplish this task?
Thank you in advance!

Cheers
StephQ

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-06-2007
StephQ wrote:

> At the moment I have a void member function (of a given class) that
> takes as input an obj of class type ofstream and write some debug
> information to it using the << operator.
> I would like to modify the code so that the function returns a
> "stream" that can be "sent" to cout or to a file using <<, as this
> would give me more flexibility.
> I used " " because I'm not sure I'm not using the correct terms
> here.


Well, cout and the file actually are the streams.

> Is there an easy way to accomplish this task?


What you can do is simply return a string that contains the debug
information. Use a stringstream within your function to fill the string.


 
Reply With Quote
 
 
 
 
StephQ
Guest
Posts: n/a
 
      03-06-2007
> What you can do is simply return a string that contains the debug
> information. Use a stringstream within your function to fill the string.


The problem (is it?) is that the debug information consists of more
than 1 line of information.
I know that with getline you can at most get an entire line bot not
more.
Does the stringstream library solves the problem?

Thanks
StephQ

p.s. sorry for the initial double post!


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-06-2007
StephQ wrote:

>> What you can do is simply return a string that contains the debug
>> information. Use a stringstream within your function to fill the string.

>
> The problem (is it?) is that the debug information consists of more
> than 1 line of information.


And why is that a problem? A std::string may contain any number of
end-of-line characters.

> I know that with getline you can at most get an entire line bot not
> more. Does the stringstream library solves the problem?


You can write multiple lines into a stringstream. The underlying string will
contain the appropriate end-of-line characters. Writing this string to any
stream will write separate lines.


Best

Kai-Uwe Bux

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
Declaring static function returning pointer to extern function pembed2012 C Programming 1 02-27-2012 08:21 PM
function returning function pointer (recursive type) Mark Piffer C Programming 9 05-15-2009 07:54 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Function returning a function pointer? Protoman C++ 14 12-11-2005 07:11 PM



Advertisments