Copy of const char * using std::string constructor

前端 未结 5 504
鱼传尺愫
鱼传尺愫 2021-01-15 09:36

is this code ok?

void SomeClass :: foo(const char * _name) {
     //name is of type const char *
     name = std::string(_name).c_str();
}

5条回答
  •  青春惊慌失措
    2021-01-15 10:11

    It's not okay to use it afterwards - the memory can be freed as soon as the temporary goes. Why don't you use a string as a member? Then you wouldn't have to worry about memory management.

提交回复
热议问题