Matthias Kaeppler wrote:
I know assignment doesn't work for a smart pointer unless the assigned
value is a smart pointer itself.
In addition the other answer, one obvious one is this: fix the smart
pointer class so that you can assign regular pointers to it which
become owned.
If you are using auto_ptr, the reset() function does that:
auto_ptr<char> x;
x.reset(new char [20]);
It's ugly, but that's what you get when a committee finally agrees on
something something: a bastard child with a dozen fathers.
A decent smart pointer class has operators that let it interoperate
smoothly with dumb pointers, such as a T * operator so that the smart
pointer objects can appear to be directly passed into functions that
expect T * pointers, equality/inequality operators so that smart
pointers can be copmared with dumb pointers, an assignment operator so
that a dumb pointer can be imported easily and becomes owned, etc.
Remember, even if the class designer didn't provide some operator, you
can write one yourself outside of the class, since operators can be
non-member functions:
template <class T>
poor_smart_ptr< T> &operator = (poor_smart_ptr <T> &lhs, T *rhs)
{
lhs = poor_smart_ptr< T>(rhs);
return lhs;
}
There, now you can assign T * pointers to a poor_smart_ptr< T> object.
See? NON-membership can have its privileges too! :)