Is this a proper use of a temporary std::string?

ぐ巨炮叔叔 提交于 2019-12-05 10:25:45

Yes, this is fine. :-)

The string will be destroyed at the end of the statement, at the semi colon.

The destructor for the temporary will not be called until after the function call returns, so what we see here is safe.

However if the called function saves the char* and it ends up being used somehow after OpenSomething has returned, then that's one fine dangling pointer.

If you don't use any other argument of OpenSomthing for returning pointer to getMyString.c_str() everything will be OK.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!