我开始学习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)