小编Mat*_*zek的帖子

shared_ptr.reset()不删除

我开始学习shared_ptr和weak_ptr.理论上一切看起来都很简单 但是当我开始测试时,嗯...我有这个非常简单的程序:

#include <iostream>
#include <memory>
using namespace std;

class Second
{
public:
    Second()
    {
        cout << "Second created" << endl;
    }
    ~Second()
    {
        cout << "Second deleted" << endl;
    }
};

class Main
{
public:
    shared_ptr<Second> second;
    Main()
    {
        cout << "Main created" << endl;
        second = make_shared<Second>(*(new Second()));
    }
    ~Main()
    {
        second.reset();
        cout << "Main deleted" << endl;
    }
};

void fun()
{
    shared_ptr<Main> main = make_shared<Main>(*(new Main()));
}

int main()
{
    cout << "Program started" << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr c++11

1
推荐指数
1
解决办法
2148
查看次数

标签 统计

c++ ×1

c++11 ×1

shared-ptr ×1