How to initialize the dynamic array of chars with a string literal in C++?

后端 未结 3 1246
心在旅途
心在旅途 2021-01-21 17:24

I want to do the following:

std::unique_ptr buffer = new char[ /* ... */ ] { \"/tmp/file-XXXXXX\" };

Obviously, it doesn\'t work

3条回答
  •  无人共我
    2021-01-21 18:00

    Since you requested a dynamic array and not wanting to count the length, that rules out std::array. What you're asking for is really just std::string - it's dynamic (if need be), and initializes just fine from a char* without counting the length. Internally, it stores the string in a flat array, so you can use it as such, via the c_str() call.

提交回复
热议问题