-
October 1st, 2003, 01:10 PM
#16
Originally posted by Yves M
You can, but it's very dangerous. The way to do it is to use const_cast as described in this FAQ item. You should not modify the buffer directly, but there may be valid reasons to do the cast. One that springs to mind is when you want to call a C-style function (from Windows, another dll etc.) that will basically never change the string, but doesn't have the const in its declaration.
Well...to go even further and to point it out clearly. You should never ever cast away the 'const' of the character string returned by a call to 'c_str()'. First of all, the supplier of the function may change it in the way that it will change the string. The main reason is simply that it is not guaranteed that the internal representation of the STL string uses consecutive memory...modifying it the way described (cast away the 'const') basically introduces undefined behaviour to your application...it might work, it might crash, it might crash 1 minute later...
So...in order to do it safely...copy the returned constant character string to a normal character string which can be modified...after modification you simply assign it back to the STL string...
-
October 2nd, 2003, 12:11 AM
#17
just wanted to be sure, is that ok?
string pszStr("Hello");
const char * pszCStr = pszStr.c_str();
char *pszStr= (char *)pszCStr;
can I modify pszStr?
-
October 2nd, 2003, 12:27 AM
#18
ignore that looking backward again, I realize it's not ok
-
October 2nd, 2003, 07:26 AM
#19
Why are you doing all this anyway? Just manipulate your "string" directly (or a copy of it as required). You need not (and normally should not) work with a C-style string unless you *have* to pass it to a legacy C-style function (one of your own hopefully). That doesn't seem to apply here so why are you troubling yourself?
-
October 2nd, 2003, 07:29 AM
#20
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 9th, 2007, 11:21 AM
#21
Re: casting from std:string to char *
string s2(s1);
I realize s2 has a constructor, but I can't tell just by looking at definition (string.h)... where does it say?
Thanks!
-
October 9th, 2007, 12:00 PM
#22
Re: casting from std:string to char *
I have to ask this because the reason i choose c# over c++ in a second is mainly because i hate c++ strings. Look how many questions get asked on just strings.
Why does the string class not give you back a c string that can be modified? and also while we are at why cant the string class convert between all string types? TCHAR, char, strings, wchar, and all of them. Maybe b strings i can see it cant switch between them but the rest are all doable so why not do it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|