Converting a C++ class to a C struct (and beyond)(将 C++ 类转换为 C 结构(及其他))
问题描述
过去几天我一直在降级"> 1000 文件的 C++ 代码到 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 结构(及其他)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 C++ 类转换为 C 结构(及其他)
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
