Re: Implementation of shared_ptr
Minkoo Seo wrote:
I've got a question on the implementation of std::tr1::shared_ptr. In
the book titled Beyond C++ Standard Library, a simple technique to
prevent the deletion of raw pointer of shared_ptr is presented:
That's a bit misleading. Making the base type's destructor protected
prevents deleting an object through a pointer to the base type,
regardless of whether that pointer came from a shared_ptr object.
[example elided]
In this example, delete raw_a raises a compile time error because A's
destructor is protected. However, the following successfully compiles
and run:
int main()
{
shared_ptr<A> a(new B());
return EXIT_SUCCESS;
}
This means that, in shared_ptr, something like the following is
happening:
As Ulrich said, the shared_ptr object internally deals with a B*, since
that's what was passed to its constructor. It knows how to delete it.
This is discussed in section 2.9 of my book, "The C++ Standard Library
Extensions: a Tutorial and Reference."
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]