std::enable...s::operator=(3)
π NAME
okay so bestie, welcome to the C++ Standard Libary manual β this page is std::enable...s::operator=(3). The NAME is literally std::enable_shared_from_this::operator= - std::enable_shared_from_this::operator=. Yes, twin names, iconic behavior. π
β¨
β¨ Synopsis β¨
Here's the whole signature vibe, since C++11:
enable_shared_from_this& operator=( const enable_shared_from_this
(since C++11)
&rhs ) noexcept;
The actual description is: Does nothing; returns *this. Like, zero drama, no cap. π«Ά
π«Ά Parameters π«Ά
rhsβ anotherenable_shared_from_thisto assign to*this. Basically the other one in the assignment situation, bestie.
π Return value π
*thisβ that's the whole return value. Simple, clean, slay. π
π§ Notes π§
The exposition-only std::weak_ptr<T> member weak-this is not affected by this assignment operator. So like, it's sitting there unbothered, main character energy. π
πΈ 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. π«Ά
// Run this code
#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:
4
π See also π
shared_ptrβ smart pointer with shared object ownership semantics (C++11) (class template). Obsessed. π
kiss kiss π β this page is from http://cppreference.com, dated 2024.06.10, and it's page std::enable...s::operator=(3). Bye bestie! π¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms