Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###Do You reall need a set size?

Do You reall need a set size?

###Delete works on nullptr

Delete works on nullptr

###Prefer Initializer list.

Prefer Initializer list.

###Do You reall need a set size?

###Delete works on nullptr

###Prefer Initializer list.

Do You reall need a set size?

Delete works on nullptr

Prefer Initializer list.

added 18 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy) // Assignment operator
 {
 Arr tmp(copy); // Make your copy (do it safely into a temp variable). If it fails this variable is not affected.
 tmp.swap(*this); // Now swap the temp and this variable so
 return *this; // this is now a copy of the input.
 } // temp variable destoryed here. Thus releasing
 // the original value.
 // The easiest way to move.
 // Is to just swap the current object with the
 // object you are moving. This way the src object
 // is guaranteed to be in a valid state (so its destructor will work)
 Arr(Arr&& move) noexcept // Move constructor.
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move) noexcept // Move assignment.
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept noexcept // swap (is no exception)
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 void resize(int newSize)
 {
 Arr tmp(newSize);
 std::copy(data, data + std::min(size, newSize), tmp.data);
 tmp.swap(*this);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
void swap(Arr& lhs, Arr& rhs)
{
 lhs.swap(rhs);
}
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy) // Assignment operator
 {
 Arr tmp(copy); // Make your copy (do it safely into a temp variable). If it fails this variable is not affected.
 tmp.swap(*this); // Now swap the temp and this variable so
 return *this; // this is now a copy of the input.
 } // temp variable destoryed here. Thus releasing
 // the original value.
 Arr(Arr&& move) // Move constructor.
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move) // Move assignment.
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept // swap (is no exception)
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 void resize(int newSize)
 {
 Arr tmp(newSize);
 std::copy(data, data + std::min(size, newSize), tmp.data);
 tmp.swap(*this);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
void swap(Arr& lhs, Arr& rhs)
{
 lhs.swap(rhs);
}
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy) // Assignment operator
 {
 Arr tmp(copy); // Make your copy (do it safely into a temp variable). If it fails this variable is not affected.
 tmp.swap(*this); // Now swap the temp and this variable so
 return *this; // this is now a copy of the input.
 } // temp variable destoryed here. Thus releasing
 // the original value.
 // The easiest way to move.
 // Is to just swap the current object with the
 // object you are moving. This way the src object
 // is guaranteed to be in a valid state (so its destructor will work)
 Arr(Arr&& move) noexcept // Move constructor.
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move) noexcept // Move assignment.
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept // swap (is no exception)
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 void resize(int newSize)
 {
 Arr tmp(newSize);
 std::copy(data, data + std::min(size, newSize), tmp.data);
 tmp.swap(*this);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
void swap(Arr& lhs, Arr& rhs)
{
 lhs.swap(rhs);
}
added 608 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy) // Assignment operator
 {
 Arr tmp(copy); // Make your copy (do it safely into a temp variable). If it fails this variable is not affected.
 tmp.swap(*this); // Now swap the temp and this variable so
 return *this; // this is now a copy of the input.
 } // temp variable destoryed here. Thus releasing
 // the original value.

 Arr(Arr&& move) // Move constructor.
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move) // Move assignment.
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept // swap (is no exception)
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 void resize(int newSize)
 {
 Arr tmp(newSize);
 std::copy(data, data + std::min(size, newSize), tmp.data);
 tmp.swap(*this);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
void swap(Arr& lhs, Arr& rhs)
{
 lhs.swap(rhs);
}
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy)
 {
 Arr tmp(copy);
 tmp.swap(*this);
 return *this;
 }
 Arr(Arr&& move)
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move)
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
class Array
{
private:
 int Size;
 int* data;
public:
 Array(int size = 0) // Have a default value and this works
 : size(size) // as a normal and default constructor.
 , data(new int[size])
 {}
 ~Array()
 {
 delete [] data;
 }
 Array(Array const& copy) // Copy constructor
 : size(copy.size)
 , data(new int[size])
 {
 std::copy(copy.data, copy.data + size, data);
 }
 Array& operator=(Array const& copy) // Assignment operator
 {
 Arr tmp(copy); // Make your copy (do it safely into a temp variable). If it fails this variable is not affected.
 tmp.swap(*this); // Now swap the temp and this variable so
 return *this; // this is now a copy of the input.
 } // temp variable destoryed here. Thus releasing
 // the original value.

 Arr(Arr&& move) // Move constructor.
 : size(0)
 , data(nullptr)
 {
 move.swap(*this);
 }
 Arr& operator(Arr&& move) // Move assignment.
 {
 move.swap(*this);
 return *this;
 }
 void swap(Arr& other) noexcept // swap (is no exception)
 {
 std::swap(data, other.data);
 std::swap(size, other.size);
 }
 void resize(int newSize)
 {
 Arr tmp(newSize);
 std::copy(data, data + std::min(size, newSize), tmp.data);
 tmp.swap(*this);
 }
 int getSize() const {return size;}
 int& operator[](int index) {return data[index];}
 int const& operator[](int index) const {return data[index];}
};
void swap(Arr& lhs, Arr& rhs)
{
 lhs.swap(rhs);
}
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /