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

前端 未结 13 1289
借酒劲吻你
借酒劲吻你 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:29

    There's always const_cast...

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

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

提交回复
热议问题