#Trouble with vector scoping

6 messages · Page 1 of 1 (latest)

rugged ingot
#
Person &findOrCreate(std::string name)
    {
        for (auto &p : _people)
        {
            if (p.getName() == name)
                return p;
        }
        _people.push_back(Person(name));
        return _people.back();
    }

Does this way of adding to the vector at the bottom allow Person(name) to stay in scope as long as the vector ?
Or am I forced to use like a shared_ptr

marsh flameBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

orchid wedge
#

it won't be the exact same object

#

but in this case it doesn't matter

#

a new object inside .push_back will be move-constructed

#

but the .back() method will work properly