forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.12.cpp
29 lines (22 loc) · 1009 Bytes
/
12.12.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
#include <memory>
using std::make_shared; using std::shared_ptr;
void process(shared_ptr<int> ptr) { }
int main() {
auto p = new int();
auto sp = make_shared<int>();
// (a) OK. Initialize a shared pointer `ptr` inside function `process` as a
// copy of `sp`, increase the reference count. After the function is
// executed, destory `ptr` and decrease the reference count.
process(sp);
// (b) Error. Cannot implicitly convert a ordinary pointer to a smart pointer.
//process(new int());
// (c) Error. Cannot implicitly convert a ordinary pointer to a smart pointer.
//process(p);
// (d) Error. Initialize a shared pointer `ptr` inside function `process`
// using ordinary pointer `p`, increase the reference count to 1. After the
// function is executed, destory `ptr` and decrease the reference count to 0,
// thus freed the memory pointed by `ptr`. However, the original ordianry
// pointer `p` still points to that memory.
//process(shared_ptr<int>(p));
return 0;
}