CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  2. #17
    Join Date
    Sep 2003
    Posts
    815
    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?

  3. #18
    Join Date
    Sep 2003
    Posts
    815
    ignore that looking backward again, I realize it's not ok

  4. #19
    Join Date
    Aug 1999
    Posts
    586
    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?

  5. #20
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Good point.
    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.

  6. #21
    Join Date
    Oct 2007
    Posts
    1

    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!

  7. #22
    Join Date
    Feb 2007
    Posts
    186

    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?

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured