5
讓我們來看看這兩個功能:如果沒有const引用它,是否會刪除臨時對象?
std::string get_string()
{
std::string ret_value;
// Calculate ret_value ...
return ret_value;
}
void process_c_string(const char* s)
{
std::cout << s << endl;
}
這裏是process_c_string
與get_string
返回參數兩種可能的調用。
沒有綁定const引用返回的對象
get_string
。process_c_string(get_string().c_str());
隨着常量引用的
get_string
返回的對象綁定。const std::string& tmp_str = get_string(); process_c_string(tmp_str.c_str());
我知道,第二個方法是有效的,但對於第一個,是什麼標準說的這種情況? get_string
返回的臨時對象會在process_c_str
之前被刪除,因爲它沒有const reference
嗎?
注意:這兩個版本都可以在MSVC中。