Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shared_ptr() = default;
- explicit shared_ptr(T *cur_ptr)
- : ptr_on_object(std::exchange(cur_ptr, nullptr)),
- use_count(new int{1}) {
- }
- explicit shared_ptr(std::nullptr_t) {
- }
- shared_ptr(const shared_ptr &other)
- : ptr_on_object(other.ptr_on_object),
- // cppcheck-suppress copyCtorPointerCopying
- use_count(other.use_count) {
- if (use_count != nullptr) {
- ++*use_count;
- }
- }
- shared_ptr(shared_ptr &&other)
- : ptr_on_object(std::exchange(other.ptr_on_object, nullptr)),
- use_count(std::exchange(other.use_count, nullptr)) {
- }
- shared_ptr &operator=(const shared_ptr &other) {
- if (other.ptr_on_object == ptr_on_object) {
- return *this;
- }
- delete_data();
- ptr_on_object = other.ptr_on_object;
- use_count = other.use_count;
- if (use_count != nullptr) {
- ++*use_count;
- }
- return *this;
- }
- shared_ptr &operator=(shared_ptr &&other) {
- if (other.ptr_on_object == ptr_on_object) {
- return *this;
- }
- delete_data();
- ptr_on_object = std::exchange(other.ptr_on_object, nullptr);
- use_count = std::exchange(other.use_count, nullptr);
- return *this;
- }
- ~shared_ptr() {
- delete_data();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement