I need to create a class like this. But when I run this code, I get: "Error in `./a.out': free(): invalid next size (fast)"
What's wrong with MyClass? How to use shared_ptr as a class member correctly?
#include <memory>
class MyClass
{
public:
MyClass(unsigned size) {
_size = size;
_arr = std::make_shared<int>(size);
for (int i = 0; i < size; i++)
_arr.get()[i] = 0;
}
MyClass(const MyClass& other) {
_arr = other._arr;
_size = other._size;
}
MyClass& operator=(const MyClass& other) {
_arr = other._arr;
_size = other._size;
}
void setArr(std::shared_ptr<int> arr, unsigned size) {
_size = size;
_arr = arr;
}
~MyClass() {
_arr.reset();
}
private:
std::shared_ptr<int> _arr;
unsigned _size;
};
int main() {
MyClass m(4);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire