Skip to content

Instantly share code, notes, and snippets.

@mudream4869
Created March 15, 2020 07:51
Show Gist options
  • Save mudream4869/c1bced567600dc1039cf9a4a089186d5 to your computer and use it in GitHub Desktop.
Save mudream4869/c1bced567600dc1039cf9a4a089186d5 to your computer and use it in GitHub Desktop.
intbuffv2.cpp
#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() {
printf("%p\n", arr);
delete[] arr;
}
private:
int* arr = nullptr;
size_t sz = 0;
};
int main() {
{
IntBuff a1(size_t(10)), b1;
b1 = a1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment