cnd
cnd

Reputation: 33784

CPtrArray without MFC (to STL or Boost)

I'm trying to leave MFC, how can I replace CPtrArray ?

maybe I can typedef it to something alike vector<void *>

What is better way to save full functional of this class ?

thank you.

Upvotes: 1

Views: 1665

Answers (2)

AAT
AAT

Reputation: 3386

You should not have been using CPtrArray in the first place. It is a dangerous old class from the dark days before Visual C++ supported templates and can only be used with enormous caution. Its problem is that it stores pointers (any pointer!) as void* and that means that when you get the pointer back out of the array you have no type information whatsoever. If your code relies heavily on this you have been at risk of misusing objects and of serious memory leaks (e.g. failing to call destructors).

boost::ptr_array is not a direct replacement for CPtrArray, it is a properly templated type-sensitive class: its nearest MFC equivalent would be CArray<T>. The fact that it uses void* to handle the pointers is (essentially) an implementation detail, and not the same at all as using vector<void*> directly.

(If you are aware of all the issues with CPtrArray then I apologise for preaching to the choir, but I thought any question about CPtrArray needed a warning note attached.)

Upvotes: 5

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99665

No need to write it yourself, you can use boost::ptr_array instead. It uses an underlying std::vector<void*> to store the pointers.

Upvotes: 1

Related Questions