确保销毁功能被称为
有人使用共享指针来确保上述句柄在不再需要时被销毁。
实际上,InitDemoStruct()
DestroyStruct()
struct DemoStruct
的实现对我来说是不透明的。它们是由其他人提供的。
虽然这段代码按预期工作,但它看起来确实很奇怪:
通常会看到智能指针指向特定的类或结构,例如:std: :shared_ptr
;最近看到这样的用法。使用智能指针指向特定类或结构的指针,例如:std::shared_ptr
;
更新:nwp
提供了答案。
这是前面提到的代码片段:
#include <memory>
#include <iostream>
struct DemoStruct;
using DemoStructHandle = DemoStruct* ;
//In practice, the implementation of `InitDemoStruct()` `DestroyStruct()` `struct DemoStruct` is opaque to me.
//They are provided by others.
struct DemoStruct{
};
void InitDemoStruct(DemoStructHandle* handle_ptr){
*handle_ptr = new DemoStruct();
//may do some other meaningful init in InitDemoStruct();
};
void DestroyStruct(DemoStructHandle* handle_ptr){
std::cout << "Destroy() is called" << std::endl;
if(handle_ptr==nullptr){
return;
}
delete *handle_ptr;
}
//I use shared pointer to ensure that the aforementioned handle is destroyed when it's no longer needed.
std::shared_ptr<DemoStructHandle> MakeDemoStructHandlePtr(void)
{
return std::shared_ptr<DemoStructHandle>(new DemoStructHandle(nullptr), [](DemoStructHandle* pHandle){
DestroyStruct(pHandle);
delete pHandle;});
}
int main()
{
std::shared_ptr<DemoStructHandle> demo_class_handle_ptr = MakeDemoStructHandlePtr();
InitDemoStruct(demo_class_handle_ptr.get());
}
Somebody use shared pointer to ensure that the aforementioned handle are destroyed when it's no longer needed.
In practice, the implementation of InitDemoStruct()
DestroyStruct()
struct DemoStruct
are opaque to me.They are provided by others.
Although this code snippet works as expected, but it looks strange indeed:
It's commonly seen that a smart pointer points to a specific class or struct, e.g: std::shared_ptr<struct DemoSt> ptr2object
; Recently, I saw such usage. Use smart pointer to pointer to a pointer of a specific class or struct, e.g:std::shared_ptr<struct DemoSt*> ptr2point_of_object
;
UPDATED: There is an answer provided by nwp
.
Here is the aforementioned code snippet:
#include <memory>
#include <iostream>
struct DemoStruct;
using DemoStructHandle = DemoStruct* ;
//In practice, the implementation of `InitDemoStruct()` `DestroyStruct()` `struct DemoStruct` is opaque to me.
//They are provided by others.
struct DemoStruct{
};
void InitDemoStruct(DemoStructHandle* handle_ptr){
*handle_ptr = new DemoStruct();
//may do some other meaningful init in InitDemoStruct();
};
void DestroyStruct(DemoStructHandle* handle_ptr){
std::cout << "Destroy() is called" << std::endl;
if(handle_ptr==nullptr){
return;
}
delete *handle_ptr;
}
//I use shared pointer to ensure that the aforementioned handle is destroyed when it's no longer needed.
std::shared_ptr<DemoStructHandle> MakeDemoStructHandlePtr(void)
{
return std::shared_ptr<DemoStructHandle>(new DemoStructHandle(nullptr), [](DemoStructHandle* pHandle){
DestroyStruct(pHandle);
delete pHandle;});
}
int main()
{
std::shared_ptr<DemoStructHandle> demo_class_handle_ptr = MakeDemoStructHandlePtr();
InitDemoStruct(demo_class_handle_ptr.get());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来像是C ++包装器周围的C ++包装器。
在一个文件中,是很奇怪的,但是当
deSostructhandle
是唯一的完整类型时,您(故意)不能做得更好。This looks like a C++ wrapper around a C opaque handle.
In one file, it is strange, but when
DemoStructHandle
is the only complete type, you (intentionally) can't do much better.我假设你的问题是如何避免双重间接(指针到指针)。您可以这样做:
shared_ptr 指向 DemoStruct,而不是指向 DemoStructHandle。
如果
InitDemoStruct
有更多参数,您可以将参数添加到MakeDemoStructPtr
I assume your question is how to avoid the double indirection (pointer-to-pointer). You can do that like this:
The shared_ptr points to a DemoStruct instead of pointing to a DemoStructHandle.
If
InitDemoStruct
has more arguments, you can add the arguments toMakeDemoStructPtr
我将使用
shared_ptr
这样:请注意,它不是
sharone_ptr; deSostructhandle&gt;
,因为您不想有一个智能指针,而是到struct的智能指针本身。如果将其包裹在出厂功能中,则不需要明确
DemoStructDeleter
,而是可以使用lambda函数。I would use a
shared_ptr
like this:Note that it is not
shared_ptr<DemoStructHandle>
, because you do not want to have a smart pointer to the handle, but to the struct itself.If you wrap this in a factory function, you need not have an explicit
DemoStructDeleter
, but can use a lambda function.我的回答:
已更新:
也许这个一个要好得多:
My answer:
UPDATED:
Maybe this one is much better: