Reescribiendo el constructor de copia con arg shared_ptr.
Tengo un constructor de copia para mi propio dynamic_array, llamado TVector.
TVector::TVector(TVector &other)
{
_size = other.length();
_array = (Square*) malloc(sizeof other);
for(int i = 0; i < other.length(); ++i){
_array[i] = other[i];
}
}
Y quiero reescribir este constructor utilizando shared_ptr, ¿cómo puedo hacer esto? Intenté esto, pero shared_ptr no proporciona el operador de subíndice.
TVector::TVector(std::shared<em>ptr<TVector> other)
{
_size = other->length();
std::shared</em>ptr<Square[]> <em>array(nullptr);
for(int i = 0; i < other->length(); ++i){
_array[i] = other[i];
}
}
Clase TVector:
class TVector {
public:
TVector();
TVector(TVector& other);
virtual ~TVector();
void Print();
void push</em>back(const Square& value);
void pop<em>back();
void clear();
int length();
Square& Last();
Square& operator[](int idx);
bool empty();
void resize(int count);
void erase(size</em>t pos);
friend std::ostream& operator<<(std::ostream& os, TVector& s);
friend std::istream& operator>>(std::istream& is, TVector& p);
int <em>size;
private:
Square *</em>array;
};
davy.ai
Puede volver a escribir el constructor de copia usando
std::shared_ptr
de la siguiente manera:TVector::TVector(std::shared_ptr<tvector> otro)
</square[]>{
_size = otro->length();
std::shared_ptr<square[]> _array(new Square[otro->length()]);
for(int i = 0; i < otro->length(); ++i){
_array.get()[i] = otro->operator[](i);
}
}