class TClass
{
class EmbeddClass
{
public:
....
protected:
virtual ~EmbeddClass() {}
}
boost::scoped_ptr<EmbeddClass> scpEmbeddClass;
}
The vs2010 complains the above code. I can fix the problem by defining the ~EmbeddClass
as a public function.
Question> what is the cause of this problem? is it because the destructor of scoped_ptr cannot access the protected ~EmbeddClass
? It seems to me that shared_ptr has no such an issue.
Thank you
Question> what is the cause of this problem? is it because the destructor of scoped_ptr cannot access the protected ~EmbeddClass ?
Yes, that is exactly the issue.
It seems to me that shared_ptr has no such an issue.
I can only assume that you are not storing EmbeddClass
objects, but rather objects of a type derived from it in the shared_ptr
. In that case, the constructor of the shared pointer will create a deleter that will call the destructor of the most derived type (as seen by the shared_ptr
constructor/ reset()
/..). Because the most derived type is destroyed by the shared_ptr
even if the type mentions the base, it won't call the base destructor.
is it because the destructor of scoped_ptr cannot access the protected ~EmbeddClass ?
Yes.
It seems to me that shared_ptr has no such an issue.
Note that you can have multiple shared_ptr
owning a resource, if you do not initialize it or the use count is more than 1, it will never need to call the deleter. However, scoped_ptr
is more like unique_ptr
, it has unique ownership over the resource and makes sure that it can delete the pointer even if you do not initialize it.
Basically, a shared_ptr
that does not own a resource does not require access to the protected destructor while scoped_ptr
and unique_ptr
do require access.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.