【发布时间】:2020-02-23 07:07:25
【问题描述】:
我的代码如下:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
struct A {
A() { cout << "c"; }
~A() { cout << "d"; }
};
int main() {
shared_ptr<void> a = make_shared<vector<A>>(3);
auto b = static_pointer_cast<vector<A>>(a);
b->push_back(A{});
return 0;
}
打印出来:
ccccdddddddd
表示析构函数被调用两次。为什么会发生这种情况以及如何解决?
【问题讨论】:
-
“插桩”成员函数时,打印
this的值
标签: c++ casting shared-ptr