Unique_ptr with an API that expects raw pointers?

After about 10 years of using managed memory and functional languages, I am finally back to C ++ and smart pointers are confusing me. Half of the documentation there is still outdated auto_ptr

.

I am trying to implement this rather simple Bullet hello world program :

int _tmain(int argc, _TCHAR* argv[])
{
    auto bp = unique_ptr<btBroadphaseInterface>(new btDbvtBroadphase);
    auto cc = unique_ptr<btDefaultCollisionConfiguration>(new btDefaultCollisionConfiguration);
    auto disp = unique_ptr<btDispatcher>(new btCollisionDispatcher(cc));
}

      

The constructor btCollisionDispatcher

wants a btCollisionConfiguration*

, but I give it to it instead unique_ptr

.

What do I usually want to do in this case? If there is a way to "de-smart" the pointer, something tells me that is unique_ptr

not the correct smart pointer to use.

enter image description here

C ++ was my language of choice before I moved on to other things. It's a little shocking to come back and see that all the patterns and practices have completely changed.

+3


source to share


2 answers


There is a member function get()

that gives you a raw pointer that is being held unique_ptr

. This does not result in unique_ptr

ownership being relinquished, however proper cleanup will be performed (be careful with storing this raw pointer!).

There is also a member function release()

that waives ownership. This means that you have returned to the ground mute pointer, and your responsibility is not responsible.



I can't figure out why the code is using new

in the first place and not just using automatic storage objects , but I'm going to pretend there is a reason ...

+13


source


The get member function returns an underling pointer and is suitable for use with existing code as long as that code does not manage memory go.



+3


source







All Articles