shared_ptr::swap



#include <memory>
#include <iostream>
                                                                                                         
using std::shared_ptr,
    std::cerr;

class Temp{
    public:
        int a, b;
        Temp(int i = 1, int j = 1):a(i), b(j){}
};

int main(){
    shared_ptr<Temp> p1(new Temp(40, 88));
    shared_ptr<Temp> p2(new Temp(32, 57));
    p1.swap(p2);
    Temp* t = p1.get();
    cerr << "p1: t->a: " << t->a << " t->b: " << t->b << "\n";
    t = p2.get();
    cerr << "p2: t->a: " << t->a << " t->b: " << t->b << "\n";

    return 0;
}      
C++ Examples© 2024 TBD