如何找到 auto_ptr 的地址
How to find the address of an auto_ptr
我有一个接受 unsigned char**
作为参数的函数。我想传递一个使用 auto_ptr 定义的参数内存。我正在尝试这样的代码:
std::auto_ptr<unsigned char> pImageBuffer;
func(&(pImageBuffer.get()));
但是,看起来我在这里做错了什么。我收到一条错误消息:
Cannot take the address of an rvalue of type 'element_type *' (aka 'unsigned char *')
有什么想法我应该怎么做?我不能使用 unique_ptr
或更改函数的签名。
听起来您传递指针地址的函数需要修改指向的地址(而不是底层对象)。您不能直接通过 auto_ptr
执行此操作(您试图获取 auto_ptr::get
的 return 值的地址 - 这是非法的,因为它是 rvalue
) .您需要先创建一个临时变量来保存原始指针。我会推荐这个:
unsigned char* tmpPtr;
func(&tmpPtr);
std::auto_ptr<unsigned char> pImageBuffer(tmpPtr);
这当然是假设该函数希望您拥有它设置 tmpPtr
指向的对象的所有权。
我有一个接受 unsigned char**
作为参数的函数。我想传递一个使用 auto_ptr 定义的参数内存。我正在尝试这样的代码:
std::auto_ptr<unsigned char> pImageBuffer;
func(&(pImageBuffer.get()));
但是,看起来我在这里做错了什么。我收到一条错误消息:
Cannot take the address of an rvalue of type 'element_type *' (aka 'unsigned char *')
有什么想法我应该怎么做?我不能使用 unique_ptr
或更改函数的签名。
听起来您传递指针地址的函数需要修改指向的地址(而不是底层对象)。您不能直接通过 auto_ptr
执行此操作(您试图获取 auto_ptr::get
的 return 值的地址 - 这是非法的,因为它是 rvalue
) .您需要先创建一个临时变量来保存原始指针。我会推荐这个:
unsigned char* tmpPtr;
func(&tmpPtr);
std::auto_ptr<unsigned char> pImageBuffer(tmpPtr);
这当然是假设该函数希望您拥有它设置 tmpPtr
指向的对象的所有权。