2

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.

2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.