Ras*_*man 4 c c++ struct class
过去几天我一直在"降级"> 1000个C++代码到C语言.直到现在一直很顺利.突然间我和一个班级面对面......
编译器首先在头文件中指出了错误:
class foobar {
foo mutex;
public:
foobar() {
oneCreate(&mutex, NULL);
}
~foobar() {
oneDestroy(mutex);
mutex = NULL;
}
void ObtainControl() {
oneAcquire(mutex);
}
void ReleaseControl() {
oneRelease(mutex);
}
};
Run Code Online (Sandbox Code Playgroud)
当然,C文件必须利用这一点
foobar fooey;
fooey.ObtainControl();
Run Code Online (Sandbox Code Playgroud)
我甚至不知道从哪里开始....帮助?
ral*_*nja 11
将foobar变成普通结构
struct foobar {
goo mutex;
};
Run Code Online (Sandbox Code Playgroud)
创建自己的"构造函数"和"析构函数"作为在该结构上调用的函数
void InitFoobar(foobar* foo)
{
oneCreate(&foo->mutex);
}
void FreeFoobar(foobar* foo)
{
oneDestroy(foo->mutex);
}
struct foobar fooStruct;
InitFoobar(&fooStruct);
// ..
FreeFoobar(&fooStruct);
Run Code Online (Sandbox Code Playgroud)
等等
由于 C 结构不能有成员函数,您可以创建函数指针,或创建这些函数的非成员版本,例如:
struct foobar {
foo mutex;
};
Construct_foobar(foobar* fooey) {
oneCreate(&fooey->mutex, NULL);
}
Destroy_foobar(foobar* fooey) {
oneDestroy(fooey->mutex);
fooey->mutex = NULL;
}
void ObtainControl(foobar* fooey) {
oneAcquire(fooey->mutex);
}
void ReleaseControl(foobar* fooey) {
oneRelease(fooey->mutex);
}
Run Code Online (Sandbox Code Playgroud)
并在 .C 文件中:
foobar fooey;
construct_foobar( &fooey );
ObtainControl( &fooey );
Run Code Online (Sandbox Code Playgroud)