Which string classes to use in C++?

后端 未结 7 2141
遇见更好的自我
遇见更好的自我 2021-02-02 14:30

we have a multi-threaded desktop application in C++ (MFC). Currently developers use either CString or std::string, probably depending on their mood. So we\'d like to choose a s

相关标签:
7条回答
  • 2021-02-02 14:54

    std::string is usually reference counted, so pass-by-value is still a cheap operation (and even more so with the rvalue reference stuff in C++0x). The COW is triggered only for strings that have multiple references pointing to them, i.e.:

    std::string foo("foo");
    std::string bar(foo);
    foo[0] = 'm';
    

    will go through the COW path. As the COW happens inside operator[], you can force a string to use a private buffer by using its (non-const) operator[]() or begin() methods.

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