Re: non destructable pointer
Manfred von Willich wrote:
You already have several replies, essentially suggesting a shared_ptr
constructed with a null-deletor for those objects that are statically
allocated. This is a good approach, but it does require the programmer
to provide an explicit null-deletor to the shared_ptr constructor.
Not a problem. You can define one for all use:
struct non_deleter
{
template < typename T >
void operator() ( T* ) {}
};
I have a different approach, used in a home-grown equivalent of boost's
intrusive_pointer, and solves the problem naturally using only
reference counting (no user-defined "no-op" deletors) - the objects can
be allocated in any way desired (static data, stack, heap).
My approach is equivalent to my reference-counting base class being
constructed with an initial count of 1, and constructing the
intrusive_ptr with a second parameter of false exactly when associated
with a "new". This manages your problem.
The up side is efficiency and coding convenience (you can construct an
intrusive_ptr from any reference/pointer - in my home-grown version
typing prevents this when not derived correctly). The down side is
having to derive from a reference counting class, and having to design
same (reinventing the wheel?).
The downside of using shared_ptr is that it is not portable if the user
at the other end is not using the same version/implementation of
shared_ptr as yourself. Now unless the standard is going to force that
there will be precisely one implementation, this can be a problem.
Intrusive shared-pointers have the advantage here that the pointers
themselves hold their reference counts so that the client code can
happily attach the pointer to their own implementation of the intrusive
pointer without any problem. It is not an unreasonable condition to
make that objects returned from a library inherit from the base class
i.e. they are "managed" objects.
In the case where you don't want the user to delete the object, you
make your class's "release" method do nothing (i.e. not call delete
this)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]