Skip to content

Commit 25a5203

Browse files
committed
Add move constructor and move assignment operator
1 parent 9e683eb commit 25a5203

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

BitmapPlusPlus.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <cstddef> // std::size_t
99
#include <string> // std::string
1010
#include <cstring> // std::memcmp
11-
#include <exception> // std::exception
11+
#include <stdexcept> // std::runtime_error
12+
#include <utility> // std::exchange
1213

1314
namespace bmp {
1415
// Magic number for Bitmap .bmp 24 bpp files (24/8 = 3 = rgb colors only)
@@ -124,6 +125,12 @@ namespace bmp {
124125

125126
Bitmap(const Bitmap &other) = default; // Copy Constructor
126127

128+
Bitmap(Bitmap &&other) noexcept
129+
: m_pixels(std::move(other.m_pixels)),
130+
m_width(std::exchange(other.m_width, 0)),
131+
m_height(std::exchange(other.m_height, 0)) {
132+
}
133+
127134
virtual ~Bitmap() noexcept {
128135
m_pixels.clear();
129136
}
@@ -400,7 +407,7 @@ namespace bmp {
400407

401408
bool operator!=(const Bitmap &image) const { return !(*this == image); }
402409

403-
Bitmap &operator=(const Bitmap &image) // Move assignment operator
410+
Bitmap &operator=(const Bitmap &image) // Copy assignment operator
404411
{
405412
if (this != &image) {
406413
m_width = image.m_width;
@@ -410,6 +417,15 @@ namespace bmp {
410417
return *this;
411418
}
412419

420+
Bitmap &operator=(Bitmap &&image) noexcept {
421+
if (this != &image) {
422+
m_pixels = std::move(image.m_pixels);
423+
m_width = std::exchange(image.m_width, 0);
424+
m_height = std::exchange(image.m_height, 0);
425+
}
426+
return *this;
427+
}
428+
413429
public: /** foreach iterators access */
414430
std::vector<Pixel>::iterator begin() noexcept { return m_pixels.begin(); }
415431

0 commit comments

Comments
 (0)