转换 C++类到 C 结构(及其他结构)

发布于 2024-11-04 11:24:01 字数 530 浏览 1 评论 0原文

这几天我一直在“降级”> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海螺姑娘 2024-11-11 11:24:02

由于 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);
}

并在 .C 文件中:

foobar fooey;
construct_foobar( &fooey );
ObtainControl( &fooey );

since C-structs can't have member functions, you can either make function pointers, or create non-member versions of those functions, ex:

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);
}

and in the .C file:

foobar fooey;
construct_foobar( &fooey );
ObtainControl( &fooey );
九歌凝 2024-11-11 11:24:02

实际上有从 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.

笑叹一世浮沉 2024-11-11 11:24:02

这取决于您的编译器,因为 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.

娇俏 2024-11-11 11:24:01

将 foobar 变成普通结构

struct foobar {
    goo mutex;
};

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

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

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

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

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);

etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文