转换一个C ++类,C结构(外) [英] Converting a C++ class to a C struct (and beyond)

查看:147
本文介绍了转换一个C ++类,C结构(外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天我已经降格> 1000 filem C ++ code变成C.
它已经顺利直到现在。突然,我面对面与一类...

Past few days I have been "downgrading" > 1000 filem of C++ code into C. It's been going well until now. Suddenly I'm face to face with a class...

编译器在头文件中首先指出了错误:

The compiler pointed out the error first in the header file:

class foobar {
    foo mutex;
public:
    foobar() {
        oneCreate(&mutex, NULL);
    }
    ~foobar() {
        oneDestroy(mutex);
        mutex = NULL;
    }
    void ObtainControl() {
        oneAcquire(mutex);
    }
    void ReleaseControl() {
        oneRelease(mutex);
    }
};

当然,C文件必须利用这一优势

And of course, the C file has to take advantage of this

foobar fooey;
fooey.ObtainControl();

我甚至不知道从哪里开始....帮助?

I don't even know where to start.... Help?

推荐答案

打开foobar的成正常结构

Turn foobar into a normal struct

struct foobar {
    goo mutex;
};

创建自己的构造和析构函数为您在该结构调用函数

Create your own "constructor" and "destructor" as functions that you call on that struct

void InitFoobar(foobar* foo)
{
   oneCreate(&foo->mutex);
}

void FreeFoobar(foobar* foo)
{
   oneDestroy(foo->mutex);
}

struct foobar fooStruct;
InitFoobar(&fooStruct);
// ..
FreeFoobar(&fooStruct);

这篇关于转换一个C ++类,C结构(外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆