-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_sharedptr.cpp
More file actions
89 lines (68 loc) · 1.24 KB
/
std_sharedptr.cpp
File metadata and controls
89 lines (68 loc) · 1.24 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* =====================================================================================
*
* Filename: shared_ptr_std.cpp
*
* Description:
*
* Version: 1.0
* Created: 11/20/2014 02:57:08 AM EST
* Revision: none
* Compiler: gcc
*
* Author: (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory>
class A
{
public:
A()
{
printf ( "A\n" );
};
~A()
{
printf ( "~A\n" );
}
void Test ( void )
{
printf ( "aaaaaaaa\n" );
}
};
std::shared_ptr<int> GetIntPtr()
{
return std::make_shared<int> ( 5 );
}
int main ( int argc, char* argv[] )
{
( void ) argc;
( void ) argv;
std::shared_ptr<int> sp ( new int[32], [] ( int* p )
{
delete p;
} );
int* p = sp.get();
for ( auto i = 0; i < 32; i++ )
{
p[i] = i;
}
for ( auto i = 0; i < 32; i++ )
{
printf ( "%d ", * ( p++ ) );
}
printf ( "\n" );
{
std::shared_ptr<A> sa = std::make_shared<A>();
sa->Test();
sa.get()->Test();
}
printf ( "test\n" );
int* pI = GetIntPtr().get();
printf ( "*pI=%d\n", *pI );
return 0;
}