-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartPointer.cpp
37 lines (27 loc) · 1.21 KB
/
smartPointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <memory>
void foo(std::shared_ptr<int> i){
(*i)++;
}
int main(){
std::shared_ptr<int> pointer = std::make_shared<int>(10);
std::shared_ptr<int> pointer2 = pointer;
std::shared_ptr<int> pointer3 = pointer;
int *p = pointer.get();
std::cout << "pointer.user_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.user_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.user_count() = " << pointer3.use_count() << std::endl;
pointer2.reset();
std::cout << "reset pointer2: " << std::endl;
std::cout << "pointer.user_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.user_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.user_count() = " << pointer3.use_count() << std::endl;
pointer3.reset();
std::cout << "reset pointer3: " << std::endl;
std::cout << "pointer.user_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.user_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.user_count() = " << pointer3.use_count() << std::endl;
foo(pointer);
std::cout << *pointer << std::endl;
return 0;
}