GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::enable_shared_from_this::operator=(3) C++ Standard Libary std::enable_shared_from_this::operator=(3)

std::enable_shared_from_this::operator= - std::enable_shared_from_this::operator=


enable_shared_from_this<T>& operator=( const (since C++11)
enable_shared_from_this<T> &obj ) noexcept;


Does nothing; returns *this.


obj - an enable_shared_from_this to assign to *this


*this


The private std::weak_ptr<T> member is not affected by this assignment operator.


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 <memory>
#include <iostream>


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() << std::endl;
}


4


shared_ptr smart pointer with shared object ownership semantics
(C++11) (class template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.