reset 以后释放了几个
运行下面这段程序:
#include <iostream>
#include <memory>
#include <string>
using namespace std;
int freed = 0;
struct Node {
string name;
unique_ptr<Node> next;
Node(string n) : name(n) {}
~Node() { freed++; }
};
int main() {
auto head = make_unique<Node>("阿岚");
head->next = make_unique<Node>("小满");
head->next->next = make_unique<Node>("阿泰");
head.reset();
cout << freed << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论