Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dumb string question 1

Status
Not open for further replies.

apatterno

Programmer
Nov 26, 2002
368
US
This question seems really stupid--to me at least. Please help.

How does one obtain a non-const pointer to a std::string's internal buffer, for example to pass to the sprintf rountine.

What I'm thinking of is along the lines of MFC's CString::GetBuffer function.

Any help is appreciated.
Will
 
I don't think you can get a non const pointer to data.

The programmer who wrote the class, probably never wanted anyone to mess around with internal data.

/JOlesen
 
So then how are you supposed to use the class at all? [lol]

For example:

std::string Caption;
Caption.resize(256);
::GetWindowTextA(hwndThis,???????????,256);

You're saying there's not a way to obtain a non-const pointer to the string's buffer in order to pass it to a function requiring a pointer to char?

I REALLY hope that helps.
Will
 
Well, maybe functions that require a non const pointer to a char should be forbidden in a C++ environment .... this shouldn't cause any problems worth mentioning in a WIN32 environment ... ;-)

 
Check out std::stringstream. That should get you around any major problems you have of that type (and pretty much replaces sprintf).

As for std::string's usability, it's perfectly usable (if badly designed) when it's used as indended: as a container of characters. That's not the only representation possible, but it's the one the STL implementors chose; possibly a little shortsighted and narrow in application, but understandable since they only had about a week to write the STL.

If it doesn't work with MFC stuff, that's 'cuz the MFC stuff wasn't designed to work with std::strings. I'd guess that's why CString exists and has capabilities you need for MFC functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top