For historical reasons, I use QSharedPointer<T>
in my software. At some points, we want to store boost::shared_ptr<T>
that point to the same data, and which should keep alive the instances of the QSharedPointer<T>
.
The common way to do this is to keep a copy of the other smart pointer within the deleter of boost::shared_ptr<T>
. But to prevent the deleter from having different types for different T
s, which would prevent easily getting a QSharedPointer
back with boost::get_deleter
, when the corrresponding boost::shared_ptr
has been upcast, I wanted to store the original QSharedPointer<T>
as a QSharedPointer<void>
within the deleter, as opposed to using the T
.
But I find that QSharedPointer
is not up to the task, as it throws errors like "reference to void can't be done", when compiling its header.
Does anyone have an idea on how to make this work without exposing T
into the deleter?
You cannot define QSharedPointer because void is not a proper type. You could define QSharedPointer and then cast to and from T * and char * Like this:
class A {};
QSharedPointer<char> x((char *)new A);
A *ptr = (A *)x.data();
Ugly but it works. You may also have to pass in a Deleter which correctly deletes the class to the constructor in order to get your class destructed correctly.
A better alternative would be to use a base class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With