快速笔记:std::shared_ptr

https://stackoverflow.com/questions/5913396/why-do-stdshared-ptrvoid-work

description

  • shared_ptr 构造时保留了析构函数指针
  • 理论上子类析构可以没有 virtual 关键字,但是不建议
  • 因为C语言的习惯,还是觉得 std::shared_ptr<void> 不应是个好的选择

code

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
#include <memory>
#include <iostream>
#include <vector>

class test {
public:
test() {
std::cout << "Test created" << std::endl;
}
~test() {
std::cout << "Test destroyed" << std::endl;
}
};

class sub_test : public test {
public:
sub_test() {
std::cout << "sub_test created" << std::endl;
}
~sub_test() {
std::cout << "sub_test destroyed" << std::endl;
}
};

int main() {
std::cout << "At begin of main.\ncreating std::vector<std::shared_ptr<void>>"
<< std::endl;
std::vector<std::shared_ptr<void>> v;
{
std::cout << "Creating test" << std::endl;
v.push_back( std::shared_ptr<test>( new test() ) );
v.push_back( std::shared_ptr<test>( new sub_test() ) );
std::cout << "Leaving scope" << std::endl;
}

std::cout << "-------------1" << std::endl;
v.pop_back(); // sub_test pop out
std::cout << "-------------2" << std::endl;
v.pop_back(); // test pop out
std::cout << "-------------3" << std::endl;

std::cout << "Leaving main" << std::endl;
return 0;
}

// output
At begin of main.
creating std::vector<std::shared_ptr<void>>
Creating test
Test created
Test created
sub_test created
Leaving scope
-------------1
sub_test destroyed
Test destroyed
-------------2
Test destroyed
-------------3
Leaving main
------ 本文结束 ------
------ 版权声明:转载请注明出处 ------