Question

While there exists an equivalent of boost::shared_ptr (QSharedPointer) I wasn't able to find something that resembles boost::shared_array.

Of course I could use something similar to

QSharedPointer<const std::vector<T> > shared_vector_ptr(new std::vector<T>(
       reinterpret_cast<T*>(pBuffer),
       reinterpret_cast<T*>(pBuffer+length)
));    

but I would like to know if there exists a native Qt solution which provides T& operator[](size_t) and uses delete[] instead of delete. I'm reluctant to use boost in this project since the target machine couldn't have boost installed and the project gets distributed by source.

Note: I know I can specify a deleter by using QSharedPointer::QSharedPointer ( T * ptr, Deleter deleter ), however I dislike approach since the compiler doesn't force you to specify a deleter, which would result in a new [] allocated block deleted by delete.

Was it helpful?

Solution

I think the nearest is QScopedArrayPointer, but of course it's scoped.

It would be little work to subclass QSharedPointer to silently add your own hidden Deleter that calls delete[] (and add anoperator[]), that way the user wouldn't have to do any thinking, and it's still using Qt native code - you have just wrapped it up neatly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top