shared_ptr的 reset用法


 

 

#include <iostream>
#include <future>
#include <thread>

using namespace std;
class Person
{
public:
    Person(int v) {
        value = v;
        std::cout << "Cons" <<value<< std::endl;
    }
    ~Person() {
        std::cout << "Des" <<value<< std::endl;
    }

    int value;

};

int main()
{
    std::shared_ptr<Person> p1(new Person(1));// Person(1)的引用計數為1

    std::shared_ptr<Person> p2 = std::make_shared<Person>(2);

 p1.reset(new Person(3));// 首先生成新對象,然后引用計數減1,引用計數為0,故析構Person(1)
                            // 最后將新對象的指針交給智能指針

    std::shared_ptr<Person> p3 = p1;//現在p1和p3同時指向Person(3),Person(3)的引用計數為2

    p1.reset();//Person(3)的引用計數為1
    p3.reset();//Person(3)的引用計數為0,析構Person(3)
    return 0;
}

 

 

 

root@ubuntu:~/c++# ./reset
Cons1
Cons2
Cons3
Des1
Des3
Des2

 

 

注意,不能將一個原始指針直接賦值給一個智能指針,如下所示,原因是一個是類,一個是指針。

    std::shared_ptr<int> p4 = new int(1);// error

  reset()包含兩個操作。當智能指針中有值的時候,調用reset()會使引用計數減1.當調用reset(new xxx())重新賦值時,智能指針首先是生成新對象,然后將就對象的引用計數減1(當然,如果發現引用計數為0時,則析構舊對象),然后將新對象的指針交給智能指針保管。

  • 獲取原始指針  
    std::shared_ptr<int> p4(new int(5));
    int *pInt = p4.get();

 

 

#include <iostream>
#include <future>
#include <thread>

using namespace std;
class Person
{
public:
    Person(int v) {
        value = v;
        std::cout << "Cons" <<value<< std::endl;
    }
    ~Person() {
        std::cout << "Des" <<value<< std::endl;
    }

    int value;

};

int main()
{
    std::shared_ptr<Person> p1(new Person(1));// Person(1)的引用計數為1

    std::shared_ptr<Person> p2 = std::make_shared<Person>(2);

    p1.reset(new Person(3));// 首先生成新對象,然后引用計數減1,引用計數為0,故析構Person(1)
                            // 最后將新對象的指針交給智能指針

    std::shared_ptr<Person> p3 = p1;//現在p1和p3同時指向Person(3),Person(3)的引用計數為2

    p1.reset();//Person(3)的引用計數為1
    p3.reset();//Person(3)的引用計數為0,析構Person(3)
    p3.reset();//再reset
    return 0;
}

 

root@ubuntu:~/c++# g++ -std=c++11 reset2.cpp -o reset2
root@ubuntu:~/c++# ./reset2
Cons1
Cons2
Cons3
Des1
Des3
Des2

 

刪除器 lambda

#include <iostream>
#include <memory>
using  namespace  std;
class obj
{
private:
     int _num;
public:
   obj(int num):_num(num) { cout << "obj is constructed and num : " << num << endl; }
   ~obj() { cout << "obj is deleted and num " <<  _num << endl; }
};
void share_ptr()
{
     std::shared_ptr<obj> shared_ptr1(new obj(99));
     obj * p = shared_ptr1.get();
     cout << " addr "  <<  (long)p << endl;
     shared_ptr1.reset(new obj(33), [](obj* p1){ cout<<"in reset"  << " addr " <<  (long)p1  <<endl;  delete p1; }); //p = shared_ptr1.get();
     //cout << " addr " <<hex << (long)p  <endl;     
}
int main()
{
   share_ptr();
   return 0;
}

 

oot@ubuntu:~/c++#  g++ -std=c++11  share2.cpp -o share2
root@ubuntu:~/c++# ./share2
obj is constructed and num : 99
 addr 187651489353328
obj is constructed and num : 33 obj is deleted and num 99
in reset addr 187651489354432
---------------------------- 先delete 99 后reset
obj is deleted and num 33

 

 

#include <iostream>
#include <memory>
using  namespace  std;
class obj
{
private:
     int _num;
public:
   obj(int num):_num(num) { cout << "obj is constructed and num : " << num << endl; }
   ~obj() { cout << "obj is deleted and num " <<  _num << endl; }
};
void share_ptr()
{
     std::shared_ptr<obj> shared_ptr1(new obj(99));
     obj * p = shared_ptr1.get();
     cout << " addr "  <<  (long)p << endl;
     shared_ptr1.reset(new obj(33), [](obj* p1){ cout<<"in reset"  << " addr " <<  (long)p1  <<endl;  });

     p = shared_ptr1.get();
     cout << " addr " << (long)p  << endl;
}
int main()
{
   share_ptr();
   return 0;
}

 

root@ubuntu:~/c++#  g++ -std=c++11  share2.cpp -o share2
root@ubuntu:~/c++# ./share2
obj is constructed and num : 99
 addr 187651132960368
obj is constructed and num : 33
obj is deleted and num 99   ---自動調用析構
 addr 187651132961472
in reset addr 187651132961472
root@ubuntu:~/c++# 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM