Can I get a non-const C string back from a C++ string?

前端 未结 13 1185
借酒劲吻你
借酒劲吻你 2020-12-01 15:33

Const-correctness in C++ is still giving me headaches. In working with some old C code, I find myself needing to assign turn a C++ string object into a C string and assign i

相关标签:
13条回答
  • 2020-12-01 16:24

    I guess there is always strcpy.

    Or use char* strings in the parts of your C++ code that must interface with the old stuff.

    Or refactor the existing code to compile with the C++ compiler and then to use std:string.

    0 讨论(0)
  • 2020-12-01 16:26

    Since c_str() gives you direct const access to the data structure, you probably shouldn't cast it. The simplest way to do it without having to preallocate a buffer is to just use strdup.

    char* tmpptr;
    tmpptr = strdup(myStringVar.c_str();
    oldfunction(tmpptr);
    free tmpptr;
    

    It's quick, easy, and correct.

    0 讨论(0)
  • 2020-12-01 16:27

    If c_str() is returning to you a copy of the string object internal buffer, you can just use const_cast<>.

    However, if c_str() is giving you direct access tot he string object internal buffer, make an explicit copy, instead of removing the const.

    0 讨论(0)
  • 2020-12-01 16:28

    in C++1x, this should work:

    foo(&s[0], s.size());
    

    However this needs a note of caution: The result of &s[0] (as the result of s.c_str(), BTW) is only guaranteed to be valid until any member function is invoked that might change the string. So you should not store the result of these operations anywhere. The safest is to be done with them at the end of the full expression, as my examples do.


    Pre C++-11 answer

    Since for to me inexplicable reasons nobody answered this the way I do now, and since other questions are now being closed pointing to this one, I'll add this here, even though coming a year too late will mean that it hangs at the very bottom of the pile...


    With C++03, std::string isn't guaranteed to store its characters in a contiguous piece of memory, and the result of c_str() doesn't need to point to the string's internal buffer, so the only way guaranteed to work is this:

    std::vector<char> buffer(s.begin(), s.end());
    foo(&buffer[0], buffer.size());
    s.assign(buffer.begin(), buffer.end());
    

    This is no longer true in C++11.

    0 讨论(0)
  • 2020-12-01 16:29

    There's always const_cast...

    std::string s("hello world");
    char *p = const_cast<char *>(s.c_str());
    

    Of course, that's basically subverting the type system, but sometimes it's necessary when integrating with older code.

    0 讨论(0)
  • 2020-12-01 16:30

    Just use const_cast<char*>(str.data())

    Do not feel bad or weird about it, it's perfectly good style to do this.

    It's guaranteed to work in C++11. The fact that it's const qualified at all is arguably a mistake by the original standard before it; in C++03 it was possible to implement string as a discontinuous list of memory, but no one ever did it. There is not a compiler on earth that implements string as anything other than a contiguous block of memory, so feel free to treat it as such with complete confidence.

    0 讨论(0)
提交回复
热议问题