-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEx12.cpp
More file actions
49 lines (36 loc) · 901 Bytes
/
Ex12.cpp
File metadata and controls
49 lines (36 loc) · 901 Bytes
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
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <memory>
using namespace std;
class Foo
{
public:
string name;
Foo(string n)
: name{move(n)}
{ cout << "CTOR " << name << '\n'; }
~Foo() { cout << "DTOR " << name << '\n'; }
};
void f(shared_ptr<Foo> sp)
{
cout << "f: use counter at " << sp.use_count() << '\n';
}
int main()
{
shared_ptr<Foo> fa;
{
cout << "Inner scope begin\n";
shared_ptr<Foo> f1 {new Foo{"foo"}};
auto f2 (make_shared<Foo>("bar"));
cout << "f1's use counter at " << f1.use_count() << '\n';
fa = f1;
cout << "f1's use counter at " << f1.use_count() << '\n';
}
cout << "Back to outer scope\n";
cout << fa.use_count() << '\n';
cout << "first f() call\n";
f(fa);
cout << "second f() call\n";
f(move(fa));
cout << "end of main()\n";
}
//program 24 for stl in c++