确保销毁功能被称为

发布于 2025-01-17 15:10:24 字数 1753 浏览 5 评论 0原文

有人使用共享指针来确保上述句柄在不再需要时被销毁。

实际上,InitDemoStruct() DestroyStruct() struct DemoStruct 的实现对我来说是不透明的。它们是由其他人提供的。

虽然这段代码按预期工作,但它看起来确实很奇怪

通常会看到智能指针指向特定的类或结构,例如:std: :shared_ptr; ptr2object;最近看到这样的用法。使用智能指针指向特定类或结构的指针,例如:std::shared_ptrptr2point_of_object;

更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2025-01-24 15:10:24

这看起来像是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.

清风挽心 2025-01-24 15:10:24

我假设你的问题是如何避免双重间接(指针到指针)。您可以这样做:

std::shared_ptr<DemoStruct> MakeDemoStructPtr(/*void not required in C++*/)
{  
    DemoStruct *pHandle;
    InitDemoStruct(&pHandle);
    return std::shared_ptr<DemoStruct>(pHandle,
        [](DemoStruct *pHandle){
            DestroyStruct(&pHandle);
        }
    );
}


int main()
{
    std::shared_ptr<DemoStruct> demo_class_handle_ptr = MakeDemoStructPtr();
}

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:

std::shared_ptr<DemoStruct> MakeDemoStructPtr(/*void not required in C++*/)
{  
    DemoStruct *pHandle;
    InitDemoStruct(&pHandle);
    return std::shared_ptr<DemoStruct>(pHandle,
        [](DemoStruct *pHandle){
            DestroyStruct(&pHandle);
        }
    );
}


int main()
{
    std::shared_ptr<DemoStruct> demo_class_handle_ptr = MakeDemoStructPtr();
}

The shared_ptr points to a DemoStruct instead of pointing to a DemoStructHandle.

If InitDemoStruct has more arguments, you can add the arguments to MakeDemoStructPtr

倾听心声的旋律 2025-01-24 15:10:24

我将使用shared_ptr这样:

struct DemoStructDeleter {
    void operator()(DemoStructHandle p) {
        DemoStructDestroy(&p);
    }
};

// in some function:
DemoStructHandle h;
InitDemoStruct(&h);
std::shared_ptr<DemoStruct> smartptr(h, DemoStructDeleter{});
// do something with smartptr

请注意,它不是sharone_ptr; deSostructhandle&gt;,因为您不想有一个智能指针,而是到struct的智能指针本身。

如果将其包裹在出厂功能中,则不需要明确DemoStructDeleter,而是可以使用lambda函数。

I would use a shared_ptr like this:

struct DemoStructDeleter {
    void operator()(DemoStructHandle p) {
        DemoStructDestroy(&p);
    }
};

// in some function:
DemoStructHandle h;
InitDemoStruct(&h);
std::shared_ptr<DemoStruct> smartptr(h, DemoStructDeleter{});
// do something with smartptr

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.

暮色兮凉城 2025-01-24 15:10:24

我的回答:

class Wrarper_without_smartptr    
{
    public:
        Wrarper_without_smartptr(){
            InitDemoStruct(&handle);
        }

        Wrarper_without_smartptr(const Wrarper_without_smartptr&) = delete;
        Wrarper_without_smartptr operator=(const Wrarper_without_smartptr&)= delete;

        ~Wrarper_without_smartptr(){
            DestroyStruct(&handle);
        }

    private:
        DemoStructHandle handle;
};

已更新
也许这个一个要好得多:

class WrapperWithSmartPointer2Handle {
      public:
        WrapperWithSmartPointer2Handle()
                : handle_ptr(new DemoStructHandle(nullptr),
                             [](DemoStructHandle* handle_ptr) { DestroyStruct(handle_ptr); }) {
            InitDemoStruct(handle_ptr.get());
        }
    
        DemoStructHandle operator*() { return *handle_ptr; }
        DemoStructHandle operator->() { return *handle_ptr;}
    
      private:
        std::shared_ptr<DemoStructHandle> handle_ptr;
    };

My answer:

class Wrarper_without_smartptr    
{
    public:
        Wrarper_without_smartptr(){
            InitDemoStruct(&handle);
        }

        Wrarper_without_smartptr(const Wrarper_without_smartptr&) = delete;
        Wrarper_without_smartptr operator=(const Wrarper_without_smartptr&)= delete;

        ~Wrarper_without_smartptr(){
            DestroyStruct(&handle);
        }

    private:
        DemoStructHandle handle;
};

UPDATED:
Maybe this one is much better:

class WrapperWithSmartPointer2Handle {
      public:
        WrapperWithSmartPointer2Handle()
                : handle_ptr(new DemoStructHandle(nullptr),
                             [](DemoStructHandle* handle_ptr) { DestroyStruct(handle_ptr); }) {
            InitDemoStruct(handle_ptr.get());
        }
    
        DemoStructHandle operator*() { return *handle_ptr; }
        DemoStructHandle operator->() { return *handle_ptr;}
    
      private:
        std::shared_ptr<DemoStructHandle> handle_ptr;
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文