Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

ostringstream(string) constructor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 14 Mar 2005 17:27 GMT
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"
Tom Widmer - 14 Mar 2005 17:29 GMT
> 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
Bob Altman - 14 Mar 2005 19:24 GMT
> >   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.
Carl Daniel [VC++ MVP] - 14 Mar 2005 20:39 GMT
>>>   string s;
>>>   ostringstream strm(s);    // This stream should store results in s
[quoted text clipped - 3 lines]
> "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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.