728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.cpp (C++11 unique_ptr, shared_ptr, weak_ptr) | |
// main code from http://www.gisdeveloper.co.kr/?p=2094 | |
#include <memory> | |
#include <iostream> | |
using namespace std; | |
void test_unique_ptr(); | |
void test_unique_ptr2(); | |
void test_shared_ptr(); | |
void test_move(); | |
void test_weak_ptr(); | |
void test_shared_weak_ptr(); | |
int main(int argc, char *argv[]) | |
{ | |
test_unique_ptr(); | |
test_unique_ptr2(); | |
test_shared_ptr(); | |
test_move(); | |
test_weak_ptr(); | |
test_shared_weak_ptr(); | |
return 0; | |
} | |
void test_unique_ptr() | |
{ | |
cout << __FUNCTION__ << endl; | |
unique_ptr<int> p1(new int(10)); // making unique pointer | |
cout << *p1 << endl; // 10 | |
} | |
void test_unique_ptr2() | |
{ | |
cout << __FUNCTION__ << endl; | |
unique_ptr<int> p1(new int(10)); | |
cout << *p1 << endl; // 10 | |
// sharing unique pointer. | |
// unique_ptr<int> p2 = p1; // error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]' | |
} | |
void test_shared_ptr() | |
{ | |
cout << __FUNCTION__ << endl; | |
shared_ptr<int> p1(new int(10)); // making shared pointer | |
shared_ptr<int> p2 = p1; // sharing shared pointer | |
cout << *p1 << endl; // 10 | |
cout << *p2 << endl; // 10 | |
cout << p2.use_count() << endl; // 2 (it's a reference count) | |
p1.reset(); | |
cout << p2.use_count() << endl; // 1 | |
p2.reset(); | |
cout << p2.use_count() << endl; // 0 | |
} | |
void test_move() | |
{ | |
cout << __FUNCTION__ << endl; | |
unique_ptr<int> p1(new int(10)); // making unique pointer | |
cout << *p1 << endl; // 10 | |
unique_ptr<int> p3 = move(p1); // moving unique pointer (it is not sharing) | |
// cout << *p1 << endl; | |
cout << p1.get() << endl; // 0 | |
cout << p3.get() << endl; // pointer | |
} | |
void test_weak_ptr() | |
{ | |
cout << __FUNCTION__ << endl; | |
shared_ptr<int> sp1(new int(10)); // making shared pointer | |
weak_ptr<int> wp1 = sp1; // sharing weak pointer | |
cout << sp1.use_count() << endl; // 1 | |
cout << wp1.use_count() << endl; // 1 | |
} | |
void test_shared_weak_ptr() | |
{ | |
cout << __FUNCTION__ << endl; | |
shared_ptr<int> sp1(new int(10)); | |
weak_ptr<int> wp1 = sp1; | |
cout << sp1.use_count() << endl; // 1 | |
cout << wp1.use_count() << endl; // 1 | |
{ | |
shared_ptr<int> sp2 = wp1.lock(); | |
cout << sp1.use_count() << endl; // 2 | |
cout << wp1.use_count() << endl; // 2 | |
} | |
cout << sp1.use_count() << endl; // 1 | |
cout << wp1.use_count() << endl; // 1 | |
} |
728x90
반응형
'C C++' 카테고리의 다른 글
[StackOverflow][Re] Create PDF document for printing in Qt from template (0) | 2018.10.22 |
---|---|
curl & libcurl 7.62.0 for Visual Studio 2017 (0) | 2018.10.06 |
gcc(g++)에서 지원하는 C++ STL version 확인하기 (0) | 2018.08.20 |
xlnt (5) msys2 에서 xlnt 빌드 하기 (0) | 2018.03.28 |
C++ 람다(lambda) 표현식 이해와 활용 예제 (0) | 2018.02.12 |