Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Obj
- {
- Obj() : mem(nullptr) {}
- ~Obj()
- {
- if(mem)
- {
- cout << "freeing" << endl;
- free(mem);
- }
- }
- Obj(Obj&& other) : mem(other.mem)
- {
- cout << "1" << endl;
- other.mem = nullptr;
- }
- Obj& operator=(Obj&& other)
- {
- cout << "2" << endl;
- if(this != &other)
- {
- mem = other.mem;
- other.mem = nullptr;
- }
- return *this;
- }
- Obj(const Obj& other) : mem(other.mem)
- {
- cout << "3" << endl;
- // other.mem = nullptr;
- }
- static Obj Create()
- {
- Obj o;
- o.mem = malloc(4 * sizeof(int));
- o.set();
- return o;
- }
- void set()
- {
- int* ptr = reinterpret_cast<int*>(mem);
- *(ptr) = 1;
- *(ptr + 1) = 2;
- *(ptr + 2) = 3;
- *(ptr + 3) = 4;
- }
- void print()
- {
- int* ptr = reinterpret_cast<int*>(mem);
- for(int i = 0; i < 4; ++i)
- cout << *(ptr + i) << " ";
- cout << endl;
- }
- void* mem;
- };
- Obj Create1()
- {
- Obj o;
- o.mem = malloc(4 * sizeof(int));
- o.set();
- return o;
- }
- int main()
- {
- Obj o(Create1());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement