Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a QSharedPointer<void>

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 Ts, 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?

like image 748
Johannes Schaub - litb Avatar asked May 26 '14 11:05

Johannes Schaub - litb


1 Answers

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.

like image 99
jcoffland Avatar answered Oct 12 '22 04:10

jcoffland