我遇到這種模式的多個實例(與升壓::文件系統只作爲例子):終身返回的字符串和它們的.c_str()
boost::filesystem::path path = ...;
someFunctionTakingCStrings(path.string().c_str());
其中
const std::string path::string() const
{
std::string tmp = ...
return tmp;
}
雖然我從來沒有遇到過這種模式的問題,但我想知道sting()
返回的字符串何時被銷燬,以及訪問c_str()
的代碼是否安全如c_str() lifetime is bound to std::string lifetime。
爲什麼它被標記爲C問題? – wasthishelpful
因爲它有c_str()在裏面 – PhilLab
@PhilLab:但是這並不意味着它會編譯*在C ;-) – Bathsheba