Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <memory>
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- class Foo {
- public:
- Foo(int size)
- : size(size)
- , arr(new int[size]) {
- for ( int i = 0; i < size; i++ ){
- arr[i] = i;
- }
- }
- Foo(const Foo& foo)
- : size(foo.size) {
- foo.foo->print();
- arr = new int[size];
- unsafe { memcpy(arr, foo.arr, sizeof(int) * size); }
- }
- ~Foo(){
- unsafe { delete[] arr; }
- }
- void print(){
- for (int i = 0; i < size; i++) {
- unsafe { std::cout << arr[i] << " "; }
- }
- std::cout << "\n";
- }
- Foo& operator=(const Foo& foo) {
- this->~Foo();
- new(this)Foo(foo);
- return *this;
- }
- void set_other(const shared_ptr<Foo>& other) {
- foo = other;
- }
- private:
- int size;
- int* arr;
- shared_ptr<Foo> foo;
- };
- int main() {
- shared_ptr<Foo> a = make_shared<Foo>(10);
- shared_ptr<Foo> b = make_shared<Foo>(20);
- b->set_other(a);
- a->print();
- b->print();
- *a = *b;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement