I have method that takes in a const vector reference. I want to put a mutable version of the vector on the heap and return that
This compiles but I am unsure if it is actually doing what I think. I am not sure if I am getting lucky with the memory here:
vector<Type> *vect = new vector<Type>;
*vect = vectReference;
It’s OK, you made dynamically allocated
vectorcopy. But the same could be done in more short way using copy constructor instead of default constructor and assignment operator:Also would like to suggest to use smart pointer instead of raw one:
std::unique_ptrorstd::shared_ptr– depending on your code semantics.