转换 C++类到 C 结构(及其他结构)
这几天我一直在“降级”> 1000 filem C++ 代码转换为 C。 到目前为止一切进展顺利。突然我和一个类面对面了...
编译器首先在头文件中指出了错误:
class foobar {
foo mutex;
public:
foobar() {
oneCreate(&mutex, NULL);
}
~foobar() {
oneDestroy(mutex);
mutex = NULL;
}
void ObtainControl() {
oneAcquire(mutex);
}
void ReleaseControl() {
oneRelease(mutex);
}
};
当然,C文件必须利用这一点
foobar fooey;
fooey.ObtainControl();
我什至不知道从哪里开始... 。 帮助?
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);
}
};
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 C 结构不能有成员函数,您可以创建函数指针,或创建这些函数的非成员版本,例如:
并在 .C 文件中:
since C-structs can't have member functions, you can either make function pointers, or create non-member versions of those functions, ex:
and in the .C file:
实际上有从 C++ 编译为 C 的编译器。不过,输出并不适合人类消化,请参阅 如何将 C++ 代码转换为 C。
There are actually compilers that compile from C++ to C. The output is not meant for human digestion, though, see How to convert C++ Code to C.
这取决于您的编译器,因为 C 中没有 RAII 的标准方法。 请参阅此问题并最佳答案。
It depends on your compiler because there isn't a standard way of RAII in C. See this question and the top answer.
将 foobar 变成普通结构
创建您自己的“构造函数”和“析构函数”作为您在该结构上调用的函数
等
Turn foobar into a normal struct
Create your own "constructor" and "destructor" as functions that you call on that struct
etc