471,075 Members | 980 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

ostringstream(string) constructor

Hi all,

Why doesn't the following unmanaged C++ code work as expected:

string s;
ostringstream strm(s); // This stream should store results in s
strm << 25;
cout << s << endl; // s still contains an empty string
cout << strm.str(); // but the stream internally contains "25"
Nov 17 '05 #1
3 1288
Bob Altman wrote:
Hi all,

Why doesn't the following unmanaged C++ code work as expected:

string s;
ostringstream strm(s); // This stream should store results in s
Where did you get the idea that the comment above is correct? The above
is just shorthand for:
ostringstream strm;
strm << s;
strm << 25;
cout << s << endl; // s still contains an empty string
Right, since you've misunderstood what the ostringstream constructor does.
cout << strm.str(); // but the stream internally contains "25"


An ostringstream doesn't hold a reference to a string, but rather is
interoperable with strings. You need:

ostringstream strm;
strm << 25;
string s(strm.str());
//etc.

Tom
Nov 17 '05 #2
> > string s;
ostringstream strm(s); // This stream should store results in s


Where did you get the idea that the comment above is correct?


"Beginning C++" by Ivor Horton, 1998 ed., page 802, states (apparently
incorrectly):

You can use an ostringstream object to format data into a string. For
instance, you could create a string object and an output string stream with
the statements:

string outBuffer;
ostringstream outStr(outBuffer);

You can now use the insertion operators to write to outBuffer via outStr:

double number = 2.5;
outStr << "number = " << (number / 2);

As a result of the write to the string stream, outBuffer will contain
"Number = 1.25"... The string parameter to the string stream constructor is
a reference, so write operations for ostringstream objects act directly on
the string object.
Nov 17 '05 #3
Bob Altman wrote:
string s;
ostringstream strm(s); // This stream should store results in s


Where did you get the idea that the comment above is correct?


"Beginning C++" by Ivor Horton, 1998 ed., page 802, states (apparently
incorrectly):


Dump that book and pick up a copy of "Accelerated C++" by Andrew Koenig and
Barbara Moo. It's 1/3 the length, has far more content, and is actually
correct.

-cd
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alex Vinokur | last post: by
6 posts views Thread by Eric Boutin | last post: by
3 posts views Thread by Mathieu Malaterre | last post: by
3 posts views Thread by Kyle Kolander | last post: by
8 posts views Thread by Bob Smith | last post: by
3 posts views Thread by Generic Usenet Account | last post: by
7 posts views Thread by Thomas Lenz | last post: by
9 posts views Thread by jl_post | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.