我用shared_ptr做了一些测试,我想不出下面的问题。我刚开始学习boost库。有人可以告诉我原因吗?
#include <boost\shared_ptr.hpp>
#include <iostream>
class A
{
public:
virtual void sing()
{
std::cout<<"A";
}
protected: virtual ~A() {};
};
class B : public A
{
public:
virtual void sing()
{
std::cout << "B";
}
virtual ~B() {};
};
int foo()
{
boost::shared_ptr<A> pa(new B());
pa->sing();
delete static_cast<B*>(pa.get());
delete pa.get(); //this line has a problem error C2248: “A::~A”: can't access protected memmber(declared in class“A")
return 0;
}
int main()
{
foo();
return 0;
}
但是当该行被注释掉时,它可以被编译。当然,这并不意味着 shared_ptr 将删除内部维护的指针,就像我所做的那样。pa.get()
返回的指针和内部维护的指针有什么区别吗?