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.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • There are two excelent posts on the subject: http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available and http://stackoverflow.com/questions/7875764/using-qsharedpointer-with-new-yields-mismatched-free-delete-delete-i – KCiebiera Jul 04 '12 at 09:34
  • Just for reference: could further downvotes explain themselves? I have no problem with downvotes, but without a comment it's hard to learn what you've done wrong. – Zeta Aug 02 '12 at 08:29

1 Answers1

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.

cmannett85
  • 21,725
  • 8
  • 76
  • 119