You made variable with name that matches type name (string variable, string type?). Plus there is issue that you return pointer to object with local scope of life. That is UB. Using iterators your function would work like this:
const string shortest_string(initializer_list<string> strings) {
if(!strings.size()) return string();
auto shortest_one = strings.begin();
for (auto it = shortest_one+1; it < strings.end(); it++ )
{
shortest_one = (it->size()< shortest_one->size()) ? it : shortest_one;
}
return *shortest_one;
}
3
solved C++ Why is my program throwing an exception?