Advertisement
kolbka_

Untitled

Jan 29th, 2022
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1.     shared_ptr() = default;
  2.     explicit shared_ptr(T *cur_ptr)
  3.         : ptr_on_object(std::exchange(cur_ptr, nullptr)),
  4.           use_count(new int{1}) {
  5.     }
  6.     explicit shared_ptr(std::nullptr_t) {
  7.     }
  8.     shared_ptr(const shared_ptr &other)
  9.         : ptr_on_object(other.ptr_on_object),
  10.           // cppcheck-suppress copyCtorPointerCopying
  11.           use_count(other.use_count) {
  12.         if (use_count != nullptr) {
  13.             ++*use_count;
  14.         }
  15.     }
  16.     shared_ptr(shared_ptr &&other)
  17.         : ptr_on_object(std::exchange(other.ptr_on_object, nullptr)),
  18.           use_count(std::exchange(other.use_count, nullptr)) {
  19.     }
  20.     shared_ptr &operator=(const shared_ptr &other) {
  21.         if (other.ptr_on_object == ptr_on_object) {
  22.             return *this;
  23.         }
  24.         delete_data();
  25.         ptr_on_object = other.ptr_on_object;
  26.         use_count = other.use_count;
  27.         if (use_count != nullptr) {
  28.             ++*use_count;
  29.         }
  30.         return *this;
  31.     }
  32.     shared_ptr &operator=(shared_ptr &&other) {
  33.         if (other.ptr_on_object == ptr_on_object) {
  34.             return *this;
  35.         }
  36.         delete_data();
  37.         ptr_on_object = std::exchange(other.ptr_on_object, nullptr);
  38.         use_count = std::exchange(other.use_count, nullptr);
  39.         return *this;
  40.     }
  41.     ~shared_ptr() {
  42.         delete_data();
  43.     }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement