I have read the this SO issue,When is it best to use the stack instead of the heap and vice versa?, It seems the stack has a limited size, so you can not alloc large object in the stack.
I have some code , which push an object to a container, see this link on online ide, http://cpp.sh/9xxea
So, should I prefer one to another ?
void AddPerson(const CPerson& p)
{
std::cout << "push back" << std::endl;
persons.push_back(p);
std::cout << "size " << persons.size() << std::endl;
}
void AddPerson()
{
CPerson cp(10, "kit");
std::cout << "push back" << std::endl;
//I know push_back will copy the element and push the copied object(which is created on heap ?), when function returns, cp will be "freed".
//so this function is better than function AddPerson(const CPerson&) ?
persons.push_back(cp);
std::cout << "size " << persons.size() << std::endl;
}
std::vector<CPerson> persons;