C++非对象类型的智能指针?
我正在尝试使用智能指针,例如 auto_ptr、shared_ptr。但是,我不知道在这种情况下如何使用它。
CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);
我不确定,但我认为存储变量只是一个 malloc 的内存,而不是 C++ 类对象。有没有办法使用智能指针来存储变量?
谢谢。
I'm trying to use smart pointers such as auto_ptr, shared_ptr. However, I don't know how to use it in this situation.
CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);
I'm not sure, but I think that the storage variable is just a malloc'ed memory, not a C++ class object. Is there a way to use the smart pointers for the storage variable?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
shared_ptr
允许您指定自定义解除分配器。但是,查看文档cvReleaseMemStorage()
没有正确的形式 (void f(T*)
),您需要一个包装器:shared_ptr
allows you do specify a custom deallocator. However, looking at the documentationcvReleaseMemStorage()
doesn't have the right form (void f(T*)
) and you need a wrapper:shared_ptr
类允许您提供自定义删除函数/函子,您可以简单地将cvReleaseMemStorage
函数包装在函数中,并为shared_ptr
提供该函数> 以及您希望它为您管理的指针?The
shared_ptr
class allows for you to provide a custom delete function/functor, you could simply wrap thecvReleaseMemStorage
function in a function and provide that forshared_ptr
along with the pointer you want it to manage for you?