Hey all,
Two part question here:
I asked for a code review a couple days ago, and on the advice of some friendly people, it was recommended I not use raw pointers. Smart pointers were entirely new to me, since all the tutorials and books I've used thus far were apparently a couple years outdated. After reading about them for a bit I tried to implement them, and the code is working, but I still wonder if I'm doing things correctly.
Basically, i need a vector of pointers to hundreds of objects. So, I've created a vector of unique_ptrs. This, of course, broke all of my functions that had that vector being passed in. So, I changed all of those functions to return a vector of unique_ptrs. And, instead of passing in the vector, i just set it to equal the returned value.
//something like this
std::vector<std::unique_ptr<Object> > Function1(std::vector<std::unique_ptr<Object> > alteredVector)
{
//do stuff
return alteredVector;
}
int main()
{
std::vector<std::unique_ptr<Object>> firstVector;
firstVector = Function1(move(firstVector));
}
This just feels a little inelegant, and since this is my first implementation of them, I worry I'm going about this the wrong way entirely. Would this be a case where I ought to use shared_ptrs instead or is this entirely fine? I think I understand the uses of the unique_ptr, but I'm a little fuzzy on shared_ptrs still.
And, then the second question, is whether I still need to clear the vector in the destructor or whether it's fine since all the pointers are deleted?
In case my example is poor, the actual implementation is here:
https://github.com/Hobogames/GameTest/blob/master/Main.cpp
https://github.com/Hobogames/GameTest/blob/master/Main.h
Thanks in advance!
Cheers :)