Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from auto_ptr to normal pointer

Tags:

c++

stl

auto-ptr

I have some third party libraries that generate and return an auto_ptr. However, I really want to use some STL containers.

So I'm guessing one way would be to convert

auto_ptr <int> ptr = some_library_call ();

into a regular c++ pointer. Will the following work?

int* myptr = ptr;

If not, what is the best way to use STL with auto_ptr (yes I know it won't work directly... I'm aware that stl and auto_ptr don't mix together)?

like image 934
owagh Avatar asked Mar 02 '26 01:03

owagh


1 Answers

You can use either ptr.get() if you want to obtain the pointer and still let the auto_ptr to delete it afterwards, or use ptr.release() to obtain the pointer and make the auto_ptr forget about it (you have to delete it afterwards.)

like image 97
Diego Sevilla Avatar answered Mar 04 '26 21:03

Diego Sevilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!