class A
{
public:
~A() { std::cout << "A" << std::endl; }
};
class B : public A
{
public:
~B() { std::cout << "B" << std::endl; }
};
template <typename T>
class MySharedV2
{
public:
template <typename T2>
MySharedV2(T2 *p)
{
data_ = p;
deleter_ = [p](){ delete p;};
}
~MySharedV2()
{
deleter_();
}
T* operator->()
{
return data_;
}
private:
std::function<void()> deleter_;
T* data_ = nullptr;
};
int main(){
MySharedV2<A> ptr(new B());
}
对于上述代码,B
的析构函数被调用我能理解,因为MySharedV2
的析构函数会调用B
的析构函数,但A
的析构函数为什么也会被调用,是什么地方调用的它?