Created
March 19, 2020 14:27
-
-
Save mudream4869/c43a11ce66059857de9063346ae359e4 to your computer and use it in GitHub Desktop.
intbuffv4.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 = std::unique_ptr<int[]>(new int[sz]); | |
} | |
}; | |
IntBuff(const IntBuff& ib) { | |
sz = ib.sz; | |
arr = std::unique_ptr<int[]>(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 = std::move(ib.arr); | |
sz = ib.sz; | |
ib.sz = 0; | |
} | |
IntBuff& operator=(IntBuff&& ib) { | |
arr = std::move(ib.arr); | |
sz = ib.sz; | |
ib.sz = 0; | |
return *this; | |
} | |
private: | |
std::unique_ptr<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