Copy Assignment
##Copy Assignment CheckingChecking for self assignment in copy assignment.
Move Assignment
##Move Assignment
TheThe standard approach is what you commented out swapping
##Move Constructor
Move Constructor
##Copy Assignment Checking for self assignment in copy assignment.
##Move Assignment
The standard approach is what you commented out swapping
##Move Constructor
Copy Assignment
Checking for self assignment in copy assignment.
Move Assignment
The standard approach is what you commented out swapping
Move Constructor
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: lengthsize{l}
, data{new int[l]}
{}
MemoryBlock(MemoryBlock const& rhs)
: lengthsize{rhs.lengthsize}
, data{new int[rhs.length]size]}
{
std::copy(rhs.data, rhs.data + lengthsize, data);
}
MemoryBlock& operator=(MemoryBlock const& rhs)
{
MemoryBlock copy(rhs);
copy.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: lengthsize{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(lengthsize, rhs.lengthsize);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return length;size;
}
private:
int length;size;
int* data;
};
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: length{l}
, data{new int[l]}
{}
MemoryBlock(MemoryBlock const& rhs)
: length{rhs.length}
, data{new int[rhs.length]}
{
std::copy(rhs.data, rhs.data + length, data);
}
MemoryBlock& operator=(MemoryBlock const& rhs)
{
MemoryBlock copy(rhs);
copy.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: length{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(length, rhs.length);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return length;
}
private:
int length;
int* data;
};
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: size{l}
, data{new int[l]}
{}
MemoryBlock(MemoryBlock const& rhs)
: size{rhs.size}
, data{new int[rhs.size]}
{
std::copy(rhs.data, rhs.data + size, data);
}
MemoryBlock& operator=(MemoryBlock const& rhs)
{
MemoryBlock copy(rhs);
copy.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: size{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(size, rhs.size);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return size;
}
private:
int size;
int* data;
};
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: length{l}
, data{new int[l]}
{}
MemoryBlock(constMemoryBlock MemoryBlock&const& rhs)
: length{rhs.length}
, data{new int[rhs.length]}
{
std::copy(rhs.data, rhs.data + length, data);
}
MemoryBlock& operator=(constMemoryBlock MemoryBlock&const& rhs)
{
MemoryBlock copy(rhs);
copy.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: length{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(length, rhs.length);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return length;
}
private:
int length;
int* data;
};
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: length{l}
, data{new int[l]}
{}
MemoryBlock(const MemoryBlock& rhs)
: length{rhs.length}
, data{new int[rhs.length]}
{
std::copy(rhs.data, rhs.data + length, data);
}
MemoryBlock& operator=(const MemoryBlock& rhs)
{
rhs.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: length{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(length, rhs.length);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return length;
}
private:
int length;
int* data;
};
class MemoryBlock
{
public:
explicit MemoryBlock(const int l = 0)
: length{l}
, data{new int[l]}
{}
MemoryBlock(MemoryBlock const& rhs)
: length{rhs.length}
, data{new int[rhs.length]}
{
std::copy(rhs.data, rhs.data + length, data);
}
MemoryBlock& operator=(MemoryBlock const& rhs)
{
MemoryBlock copy(rhs);
copy.swap(*this);
return *this;
}
MemoryBlock(MemoryBlock&& rhs) noexcept
: length{0}
, data{nullptr}
{
rhs.swap(*this);
}
MemoryBlock& operator=(MemoryBlock&& rhs) noexcept
{
rhs.swap(*this);
return *this;
}
void swap(MemoryBlock& rhs) noexcept
{
using std::swap;
swap(length, rhs.length);
swap(data, rhs.data);
}
friend void swap(MemoryBlock& lhs, MemoryBlock& rhs)
{
rhs.swap(lhs);
}
~MemoryBlock()
{
delete [] data;
}
int length() const
{
return length;
}
private:
int length;
int* data;
};