# std::enable_shared_from_this<T>::operator=

> C++ Atlas: `cppatlas.com/w/cpp/memory/enable_shared_from_this/operator=` | Source: https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/operator= | Domain: Memory management | Standards: C++11

Does nothing; returns *this.

## Official Specification Summary (cppreference)

```cpp
enable_shared_from_this& operator=( const enable_shared_from_this &rhs ) noexcept; // (since C++11)
```

Does nothing; returns `*this`.

### Parameters

- `rhs` - another `enable_shared_from_this` to assign to `*this`

### Return value

`*this`

### Notes

`weak_this` is not affected by the assignment.

### Example

Note: `enable_shared_from_this::operator=` is defined as `protected` in order to prevent accidental slicing but allow derived classes to have default assignment operators.

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

class SharedInt : public std::enable_shared_from_this<SharedInt>
{
public:
    explicit SharedInt(int n) : mNumber(n) {}
    SharedInt(const SharedInt&) = default;
    SharedInt(SharedInt&&) = default;
    ~SharedInt() = default;

    // Both assignment operators use enable_shared_from_this::operator=
    SharedInt& operator=(const SharedInt&) = default;
    SharedInt& operator=(SharedInt&&) = default;

    int number() const { return mNumber; }

private:
    int mNumber;
};

int main()
{
    std::shared_ptr<SharedInt> a = std::make_shared<SharedInt>(2);
    std::shared_ptr<SharedInt> b = std::make_shared<SharedInt>(4);
    *a = *b;

    std::cout << a->number() << '\n';
}
```

Output:

```text
4
```

### See also

- [`shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr) (C++11): smart pointer with shared object ownership semantics (class template)

---
Attribution: content from https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/operator= (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.