在C ++中为C风格的对象创建透明的包装类

时间:2022-02-01 22:28:03

I want to implement a class in C++ whose purpose is implementing RAII mechanism for a C-style object.

我想用C ++实现一个类,其目的是为C风格的对象实现RAII机制。

Then, I need to be able to pass an instance of this class to all the C-style functions that receive the mentioned C-style object as argument. I know that this should be solved with unique_ptr, but I cannot use C++11 for now. Anyway, I would like to understand how this should be made, regardless that there are better solutions.

然后,我需要能够将此类的实例传递给所有接收上述C样式对象作为参数的C样式函数。我知道这应该用unique_ptr解决,但我现在不能使用C ++ 11。无论如何,我想了解如何做到这一点,无论有更好的解决方案。

I have several doubts regarding to which operators I must overload, and the difference beteween some of them.

我有几个疑问,我必须重载哪些运算符,以及它们之间的差异。

Below is an example of code with the implementation I have made. I am specially confused with the operators 1 and 2 (what is the difference?)

下面是我所做的实现的代码示例。我特别想与运营商1和2混淆(有什么区别?)

I want to know if the code I have implemented covers all the use cases, I mean, all the scenarios where the C library could use the object. In addition, I would like to understand the difference between the operators 1 and 2.

我想知道我实现的代码是否涵盖了所有用例,我的意思是,C库可以使用该对象的所有场景。另外,我想了解运算符1和2之间的区别。

C-Style object

// Example: a C-style library called "clib" which use an object called "cobj":

struct cobj {
    int n;    
};

void clib_create_cobj(struct cobj **obj) {
    *obj = (struct cobj*)malloc(sizeof(cobj));
    (*obj)->n = 25;
}

void clib_release_cobj(struct cobj *obj) {
    free(obj);
}

void clib_doSomething(struct cobj *obj) {
    std::cout << obj->n << std::endl;
}

C++ "transparent" wrapper

// My wrapper class for implementing RAII

class CobjWrapper {
    public:
        CobjWrapper(struct cobj *obj) : m_obj (obj) { }

        ~CobjWrapper() {
            if(m_obj != NULL) {
                clib_release_cobj(m_obj);
                m_obj = NULL;
            }
        }


        operator struct cobj* () const {    // (1)
            return m_obj;
        }

        struct cobj& operator * () {        // (2)
            return *m_obj;
        }

        struct cobj** operator & () {       // (3)
            return &m_obj;
        }

        struct cobj* operator->() {     // (4)
            return m_obj;
        }

    private:
        struct cobj *m_obj;

};

The main method

// The main method:

int main() {
  struct cobj *obj = NULL;

  clib_create_cobj(&obj);

  CobjWrapper w(obj);
  clib_doSomething(w);

  return 0;
}

The above source can be tested here:

上面的来源可以在这里测试:

http://cpp.sh/8nue3

1 个解决方案

#1


3  

The following is an implicit cast

以下是隐式演员

operator struct cobj* () const {    // (1)

with example usage

使用示例

CobjWrapper wrapper = /**/;

struct cobj* obj = wrapper;

whereas the following is the unary operator *

而以下是一元运算符*

struct cobj& operator * () {        // (2)

with example usage

使用示例

CobjWrapper wrapper = /**/;

struct cobj& obj = *wrapper;

BTW, I would completely hide the C struct, something like:

顺便说一句,我会完全隐藏C结构,如:

class CobjWrapper {

    struct ObjDeleter
    {
        void operator()(cobj *obj) const { clib_release_cobj(obj); }
    };

    public:
        CobjWrapper()
        {
             cobj *obj = nullptr;
             clib_create_cobj(&obj);
             m_obj.reset(obj);
        }

        void doSomething() { clib_doSomething(m_obj.get()); }

    private:
        std::unique_ptr<cobj, ObjDeleter> m_obj;
};

#1


3  

The following is an implicit cast

以下是隐式演员

operator struct cobj* () const {    // (1)

with example usage

使用示例

CobjWrapper wrapper = /**/;

struct cobj* obj = wrapper;

whereas the following is the unary operator *

而以下是一元运算符*

struct cobj& operator * () {        // (2)

with example usage

使用示例

CobjWrapper wrapper = /**/;

struct cobj& obj = *wrapper;

BTW, I would completely hide the C struct, something like:

顺便说一句,我会完全隐藏C结构,如:

class CobjWrapper {

    struct ObjDeleter
    {
        void operator()(cobj *obj) const { clib_release_cobj(obj); }
    };

    public:
        CobjWrapper()
        {
             cobj *obj = nullptr;
             clib_create_cobj(&obj);
             m_obj.reset(obj);
        }

        void doSomething() { clib_doSomething(m_obj.get()); }

    private:
        std::unique_ptr<cobj, ObjDeleter> m_obj;
};

相关文章