the idea is that I want a class which is wrapped by std::shared_ptr, can still be used
just like they weren’t a pointer, for example the operator= which was defined in my class
can still be used after my class is wrapped by std::shared_ptr.
for example
template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty> {
public:
template<class Other> shared_ptr_proxy& operator=(const Other& rhs)
{
(*this->get()) = rhs;
return *this;
}
template<class Other> explicit shared_ptr_proxy(Other * ptr)
: std::shared_ptr<Ty>(ptr){};
};
// usage :
shared_ptr_proxy<float> obj = shared_ptr_proxy<float>(new float);
obj = 3.14;
its work, but is there a way that i don’t need to create shared_ptr_proxy or
inheriting a class from std::shared_ptr ?
and
if I do like this, is there a caveat that i should take care of?
It depends on what you want the proxy for. A full proxy might make it look entirely like you had the value, so you’d provide the conversion operators.
In such case, it might not be a good idea to inherit from
shared_ptr, though, because you might be inheriting functions that you want to rely on the implicit conversions instead.Compare how sorting orders the items: