-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoPtr.cpp
More file actions
37 lines (30 loc) · 1.03 KB
/
autoPtr.cpp
File metadata and controls
37 lines (30 loc) · 1.03 KB
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
/**
* http://suanfa.blogspot.com/2010/10/auto-ptr.html
*
*/
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main() {
int *p1 = new int(3);
auto_ptr<int> p2(p1);
// still works because copy from pointer to smart pointer
cout<<*p1<<endl;
cout<<*p2<<endl;
auto_ptr<int> p3(new int(4));
auto_ptr<int> p4(p3);
cout<<*p4<<endl;
// cout<<*p3<<endl; // Segmentation fault, coz p3 is NULL
vector<auto_ptr<int> > v;
v.push_back(p1); // error, can not push int * to auto_ptr
v.push_back(p4); // doesn't compile
// auto_ptr has a copy constructor with a non-const parameter,
// so the compiler can't call it from vector::push_back() since the latter has const parameter.
// The reason is when you initialize one auto_ptr instance from
// another the new instance disconnects the object from
// the other instance and connects it to self
// so avoid a dangling pointer situation when one instance deletes the object and
// the other still holds a pointer to it.
return 1;
}