mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
prepare for c++17
This commit is contained in:
41
code/5/5.2.cpp
Normal file
41
code/5/5.2.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
struct Foo {
|
||||
Foo() { std::cout << "Foo::Foo" << std::endl; }
|
||||
~Foo() { std::cout << "Foo::~Foo" << std::endl; }
|
||||
void foo() { std::cout << "Foo::foo" << std::endl; }
|
||||
};
|
||||
|
||||
void f(const Foo &) {
|
||||
std::cout << "f(const Foo&)" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::unique_ptr<Foo> p1(std::make_unique<Foo>());
|
||||
|
||||
// p1 不空, 输出
|
||||
if (p1) p1->foo();
|
||||
{
|
||||
std::unique_ptr<Foo> p2(std::move(p1));
|
||||
|
||||
// p2 不空, 输出
|
||||
f(*p2);
|
||||
|
||||
// p2 不空, 输出
|
||||
if(p2) p2->foo();
|
||||
|
||||
// p1 为空, 无输出
|
||||
if(p1) p1->foo();
|
||||
|
||||
p1 = std::move(p2);
|
||||
|
||||
// p2 为空, 无输出
|
||||
if(p2) p2->foo();
|
||||
std::cout << "p2 被销毁" << std::endl;
|
||||
}
|
||||
// p1 不空, 输出
|
||||
if (p1) p1->foo();
|
||||
|
||||
// Foo 的实例会在离开作用域时被销毁
|
||||
}
|
||||
Reference in New Issue
Block a user