# Smart Pointers without Reference Counting

```cpp
#include <memory>
```

C++ utilities library provides us three kinds of smart pointers without reference counting: *auto\_ptr*, *scoped\_ptr* and *unique\_ptr*. These pointers have a common feature, that at the same time there can only be one pointer that manages the object. In other words, every time a new copy of a pointer transfer the ownership of objects from the previous one.

### *auto\_ptr*

*auto\_ptr* rewrites the copy constructor so that every time a new copy of the pointer is created, it takes handle of its object, and the original pointer is set to NULL. In this way, there can only be one pointer pointing to the object. In this way, the destructor only need to deal with the only pointer pointing to the object. Therefore, we can not use the original pointer any more if its ownership has been transferred:

```cpp
int main() {
    auto_ptr<int> p1(new int);
    auto_ptr<int> p2(p1);
    *p2 = 20;
    cout << *p1 << endl;    // ERROR
    return 0;
}
```

*auto\_ptr* has to be used carefully, for programmers sometimes may not realize the transfer of ownership and invalidate the pointer unintentionally,.

### *scoped\_ptr*

*scoped\_ptr* strictly regulates that there is always one pointer pointing to an object. It achieves this by disabling the copy constructor of operator`=`:

```cpp
scoped_ptr(const scoped_ptr<T> &) = delete;
scpoed_ptr<T> &operator=(const scoped_ptr<T> &) = delete;
```

In this case, any copy of a *scoped\_ptr* is not allowed:

```cpp
int main() {
    scoped_ptr<int> p1(new int);
    scoped_ptr<int> p2(p1); // ERROR
    return 0;
}
```

Since *scoped\_ptr* is to strict towards the ownership, it is not widely used.使用的比较少

### *unique\_ptr*

*unique\_ptr* disables the copy constructor and operator`=` as well. But instead, it provides a copy constructor and operator`=` with Rvalue references:

```cpp
unique_ptr(const unique_ptr<T> &) = delete;
unique_ptr<T> &operator=(const unique_ptr<T> &) = delete;
unique_ptr(unique_ptr<T> &&);
unique_ptr<T> &operator=(unique_ptr<T> &&);
```

In this case, we still cannot copy a pointer directly, but we can use the move semantics here:

```cpp
int main() {
    unique_ptr<int> p1(new int);
    unique_ptr<int> p2(std::move(p1));
    return 0;
}
```

Now *p1* transfer the ownership of the object to *p2*, and then be set to NULL. Again, the destructor only needs to deal with *p2*.

*unique\_ptr* has almost the same functionality as *auto\_ptr*, but is more recommended because its semantic is clearer. Under normal circumstances, copying a *unique\_ptr* is not allowed, and we must explicitly use *std::move*. Hence, it will not happen that the programmer unintentionally transfer the ownership of a pointer, which is much safer.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://navining.gitbook.io/gocpp/chapter-10-smart-pointers/smart-pointers-without-reference-counting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
