mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
50
code/5/5.2.unique.ptr.cpp
Normal file
50
code/5/5.2.unique.ptr.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// 5.2.unique.ptr.cpp
|
||||
// chapter 05 start pointers and memory management
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
// https://github.com/changkun/modern-cpp-tutorial
|
||||
//
|
||||
|
||||
#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 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
{
|
||||
std::unique_ptr<Foo> p2(std::move(p1));
|
||||
|
||||
// p2 is not empty, prints
|
||||
f(*p2);
|
||||
|
||||
// p2 is not empty, prints
|
||||
if(p2) p2->foo();
|
||||
|
||||
// p1 is empty, no prints
|
||||
if(p1) p1->foo();
|
||||
|
||||
p1 = std::move(p2);
|
||||
|
||||
// p2 is empty, no prints
|
||||
if(p2) p2->foo();
|
||||
std::cout << "p2 was destroied" << std::endl;
|
||||
}
|
||||
// p1 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
|
||||
// Foo instance will be destroied when leaving the scope
|
||||
}
|
||||
Reference in New Issue
Block a user