I have been reading up on the c++ auto_ptr and unique_ptr and stuff and thought to try and use them in a class I am playing with… but I was having trouble getting it to work…
How would I convert these pointers to auto pointers or some equivalent so the deletion of the pointers is handled automatically?
Header – http://ideone.com/Z9bc5
Body – http://ideone.com/WfwBY
At the moment it is working using normal pointers but I sometimes get a access violation error. I am pretty sure I know what it causing it.. but the “best” way might be to use the automatic deletion stuff recently added to c++11?
Thanks in advance.
Don’t use
auto_ptr. Try one ofunique_ptrorshared_ptr. Here’s Sutter explaining when to use which:Also from his blog-post:
So, your member declarations change from:
to:
As for member functions which return a raw pointer you have but the obvious choice: You cannot return a
unique_ptrif the class is not movable. So, you can either:const& unique_ptr<T>Choose the one that suits your needs the best.