std :: unique_ptr和指向指针的指针 [英] std::unique_ptr and pointer to pointer
本文介绍了std :: unique_ptr和指向指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 std :: unique_ptr
与FreeImage的FITAG组合。普通C中的代码是:
I want to use std::unique_ptr
in combination with FreeImage's FITAG. The code in plain C would be:
... load image;
FITAG* tag = NULL;
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &tag);
... do some stuff with tag;
FreeImage_DeleteTag(tag);
... delete image;
我尝试使用unique_ptr:
My attempt with unique_ptr:
std::unique_ptr<FITAG, void(*)(FITAG*)> tag(NULL, &FreeImage_DeleteTag);
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &tag.get());
这显然会返回:
cannot take the address of an rvalue of type 'pointer' (aka 'FITAG *')
我如何解决这个问题?
提前多谢。
推荐答案
颠倒操作顺序;首先获取资源,然后构建 unique_ptr
。
Reverse the order of operations; first acquire the resource and then construct the unique_ptr
.
FITAG *p = NULL;
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &p);
std::unique_ptr<FITAG, void(*)(FITAG*)> tag(p, &FreeImage_DeleteTag);
这篇关于std :: unique_ptr和指向指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文