I have a class, DevicePointer
, that encapsulates a std::shared_ptr<Device>
. Various classes that need to hold a pointer to a Device derive from DevicePointer
. Before I started using shared_ptr
, DevicePointer
had an ::Expose()
function that would return a raw pointer to a Device. Now I'm using shared_ptr to hold the Device pointer, I'm not sure how to return it. Note that the only reason ::Expose
should be called is to dereference the pointer.
This is what the original Expose looked like:
Device * Expose() const { return MyDevice; }
and would be used like this:
Device::Expose()->ExecuteFunction(a, b, c);
Now MyDevice
is a std::shared_ptr<Device>
, I'm not sure how to return it for dereferencing. The obvious choice is:
std::shared_ptr<Device> Expose() { return MyDevice; }
but I worry about performance, particularly the creation of a new temporary std::shared_ptr
. So I need some way of saying "you can dereference this pointer but you can't copy it". The original still needs to be shared because many objects will hold a reference to it.
I hope I've articulated my question adequately. Thanks.