简体   繁体   中英

Casting auto_ptr to void pointer

I am trying to cast auto_ptr to void pointer in the following manner:

void *AM::This2Ctx(std::auto_ptr<AMContext> data)
{
 return reinterpret_cast<void *>(data);
}

but i keep getting a compilation error:

error: invalid cast from type std::auto_ptr<AMContext> to type void*

how can this casting be done correctly? and how can it be used in the opposite way?

Use .get() to access the pointer held by auto-ptr :

reinterpret_cast<void *>(data.get());
                             ~~~~~~

In addition, auto_ptr is deprecated, use unique_ptr instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM