Let there be two functions
std::string f(); void g(const char*); which i can't change. Are the following ways to use them different?
{ g(f().c_str()); } // 1 { g(f().data()); } // 2 { std::string tmp = f(); g(tmp.c_str()); } // 3 { std::string tmp = f(); g(tmp.data()); } // 4 I would be grateful for a detailed explanation of the work of the internal kitchen in Slaves 1 and 2. A temporary variable of the type std::string , a pointer to its data is taken, passed to g() . When is this temporary variable destroyed?
Thank you answered. I will add a couple of links to EN.stackoverflow, there are references to the standard:
data()instead ofc_str()only available starting from C ++ 11. Until C ++ 11, it was not guaranteed thatdata()would return a C-string. - AnTdataandc_strare full equivalents? - andy.37