Created
March 18, 2020 06:40
-
-
Save mudream4869/061f560a59e3b450d14dbc8454335523 to your computer and use it in GitHub Desktop.
intbuffv3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cstdio> | |
#include <memory> | |
class IntBuff { | |
public: | |
IntBuff() = default; | |
explicit IntBuff(size_t sz) : sz(sz) { | |
if (sz) { | |
arr = new int[sz]; | |
} | |
}; | |
IntBuff(const IntBuff& ib) { | |
sz = ib.sz; | |
arr = new int[sz]; | |
for (int lx = 0; lx < sz; lx++) { | |
arr[lx] = ib.arr[lx]; | |
} | |
} | |
IntBuff& operator=(const IntBuff& ib) { | |
IntBuff tmp(ib); // copy-and-swap | |
std::swap(tmp.arr, arr); | |
std::swap(tmp.sz, sz); | |
return *this; | |
} | |
IntBuff(IntBuff&& ib) { | |
arr = ib.arr; | |
ib.arr = nullptr; | |
sz = ib.sz; | |
ib.sz = 0; | |
} | |
IntBuff& operator=(IntBuff&& ib) { | |
delete arr; | |
arr = ib.arr; | |
ib.arr = nullptr; | |
sz = ib.sz; | |
ib.sz = 0; | |
return *this; | |
} | |
~IntBuff() { | |
printf("%p\n", arr); | |
delete[] arr; | |
} | |
private: | |
int* arr = nullptr; | |
size_t sz = 0; | |
}; | |
int main() { | |
{ | |
IntBuff a1(size_t(10)), b1; | |
b1 = std::move(a1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment