从shared_pointer创建weak_pointer并重用基本指针

时间:2019-07-15 10:44:05

标签: c++ pointers shared-ptr weak-ptr

为什么wp5不指向值为Integer的{​​{1}}?

50

是否可以创建使用#include <memory> #include <iostream> #include "DoubleLinkedList.h" class Integer { private: int number; public: Integer(int number) : number(number) {} int get() { return number; } }; int main() { Integer i1(10); Integer i2(20); i1 = i2; std::cout << "i1 = " << i1.get() << std::endl; Integer* i3 = new Integer(30); std::shared_ptr<Integer> sp3 = std::make_shared<Integer>(i1); std::shared_ptr<Integer> sp4 = std::shared_ptr<Integer>(new Integer(40)); std::weak_ptr<Integer> wp5 = std::shared_ptr<Integer>(new Integer(50)); std::weak_ptr<Integer> wp6 = sp4; return 0; } 作为指向shared_pointer<Integer>的基本指针的weak_pointer<Integer>*i3

0 个答案:

没有答案