이 코드는 안전한가요? 오류없이 실행되기 때문에 나는 자신을 위해 함정을 세우고 있다고 우려하고 있습니다.반환 된 std :: string에 ptr 전달
void targetMethod(const char *arg);
std::string helperMethod(char *text) { return std::string(text); }
targetMethod(helperMethod().c_str());
helperMethod()는 표준 : : 문자열을 반환하고, 호출 코드는 기본 문자의 *를 가져오고() targetMethod에 전달합니다. 나는 반환 된 문자열이 일시적이므로 기본 char *를 가져 오는 것이 위험하다는 것에 우려하고 있습니다. 그래서 그 대신 I는 다음과 같이 수행해야합니다
std::string myTemp = helperMethod("hello");
targetMethod(myTemp.c_str());
고마워요, 코디. 내 수색을 더 세밀하게해야 해. – MJF