I had a whole pile of trouble with objects getting destroyed(and their heap data along with them) when returning vectors from functions a while back. I don't remember the exact details so I tried returning a vector of objects today and their destructor didn't get triggered.
Do I remember it wrong? If i made a vector like this:
std::vector<myObject> MakeVectorOfMyObjects(int size) {
std::vector<myObject> ret;
for (int i = 0; i < size; i++) {
ret.push_back(myObject());
}
return ret;
}
and called it like that:
std::vector<myObject> stuff = MakeVectorOfMyObjects(5);
Is it guaranteed that the the vector that now resides in "stuff" is the exact same as the one i built in the function without any of the objects getting destroyed, remade or otherwise manipulated during the return and assignment?
Also does it make any difference whether I pass a vector to a function by reference or by value?
EDIT: getting a bit vague answers so let me rephrase: How to I guarantee that the the vector in "stuff" is the same pile of bits as the one I created in the function? (outside creating the vector with new keyword and returning the pointer that is)