Duplicate as shared_ptr to an array : should it be used?
According to this post, the good way to wrap a array with smart_ptr is to define a deleter function and pass the deleter function to smart_ptr alone with raw arrays.
I'm going to refactor my code, like wrap raw arrays with smart_ptr. Here's a example:
Original codes:
class MyList{
public:
MyList(int size = 0);
void Resize(int size);
~MyList();
private:
int* myArray;
int* head_;
size_t size_;
}
MyList::MyList(int size){
myArray = new int[size]; //allocated memory when object is being created
head_ = list_;
size_ = size;
}
void MyList::Resize(int size) {
if (!list_) {
delete myArray;
size_ = 0;
}
myArray = new int[size];
head_ = list_;
size_ = size;
}
MyList::~MyList(){
delete myArray;
head = null_ptr;
}
My question is:
How to correctly wrap raw arrays with smart_ptr
?