Delen via


shared_ptr::operator=

Replaces the owned resource.

shared_ptr& operator=(const shared_ptr& sp);
template<class Other>
    shared_ptr& operator=(const shared_ptr<Other>& sp);
template<class Other>
    shared_ptr& operator=(auto_ptr<Other>& ap);
template<class Other>
    shared_ptr& operator=(auto_ptr<Other>& ap);
template<class Other>
    shared_ptr& operator=(auto_ptr<Other>&& ap);
template<class Other, class Deletor>
    shared_ptr& operator=(unique_ptr<Other, Deletor>&& ap);

Parameters

  • sp
    The shared pointer to copy.

  • ap
    The auto pointer to copy.

Remarks

The operators all decrement the reference count for the resource currently owned by *this and assign ownership of the resource named by the operand sequence to *this. If the reference count falls to zero, the resource is released. If an operator fails it leaves *this unchanged.

Example

 

// std_tr1__memory__shared_ptr_operator_as.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::shared_ptr<int> sp0; 
    std::shared_ptr<int> sp1(new int(5)); 
    std::auto_ptr<int> ap(new int(10)); 
 
    sp0 = sp1; 
    std::cout << "*sp0 == " << *sp0 << std::endl; 
 
    sp0 = ap; 
    std::cout << "*sp0 == " << *sp0 << std::endl; 
 
    return (0); 
    } 
 
*sp0 == 5
*sp0 == 10

Requirements

Header: <memory>

Namespace: std

See Also

Reference

shared_ptr Class

shared_ptr::reset