-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsharedPtr.cpp
39 lines (29 loc) · 881 Bytes
/
sharedPtr.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
38
39
#include <iostream>
#include <memory>
class MyInt{
public:
MyInt(int v):val(v){
std::cout << " Hello: " << val << '\n';
}
~MyInt(){
std::cout << " Good Bye: " << val << '\n';
}
private:
int val;
};
int main(){
std::cout << '\n';
std::shared_ptr<MyInt> sharPtr(new MyInt(1998));
std::cout << "sharedPtr.use_count(): " << sharPtr.use_count() << '\n';
{
std::shared_ptr<MyInt> locSharPtr(sharPtr);
std::cout << "locSharPtr.use_count(): " << locSharPtr.use_count() << '\n';
}
std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << '\n';
std::shared_ptr<MyInt> globSharPtr = sharPtr;
std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << '\n';
globSharPtr.reset();
std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << '\n';
sharPtr = std::shared_ptr<MyInt>(new MyInt(2011));
std::cout << '\n';
}