MCPcopy Create free account
hub / github.com/changkun/modern-cpp-tutorial / main

Function main

code/5/5.2.cpp:14–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12}
13
14int main() {
15 std::unique_ptr<Foo> p1(std::make_unique<Foo>());
16
17 // p1 不空, 输出
18 if (p1) p1->foo();
19 {
20 std::unique_ptr<Foo> p2(std::move(p1));
21
22 // p2 不空, 输出
23 f(*p2);
24
25 // p2 不空, 输出
26 if(p2) p2->foo();
27
28 // p1 为空, 无输出
29 if(p1) p1->foo();
30
31 p1 = std::move(p2);
32
33 // p2 为空, 无输出
34 if(p2) p2->foo();
35 std::cout << "p2 被销毁" << std::endl;
36 }
37 // p1 不空, 输出
38 if (p1) p1->foo();
39
40 // Foo 的实例会在离开作用域时被销毁
41}

Callers

nothing calls this directly

Calls 2

fFunction · 0.85
fooMethod · 0.45

Tested by

no test coverage detected