便于在现有C实现中使用C ++ [英] Facilitate the use of C++ within an existing C implementation

查看:153
本文介绍了便于在现有C实现中使用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我被分配了一个任务来提供我的C ++代码的C实现。由于原始代码很大,并且很大程度上依赖于C中不存在的结构,我不知道
是什么是最简单的传输方式。

Recently I was assigned a task to provide a C implementation of my C++ code. Since the original code is large, and is largely relying on structures not present in C, I wonder what would be the easiest way for the transfer.

I读取有包装器允许在C中使用C ++?这些允许我简单地
复制/粘贴代码在C?

I read that there are wrappers that allow one to use C++ in C? Will these allow me to simply copy/paste the code in C?

此外,我探索了以下
http://attractivechaos.wordpress.com/2008/09/19/c-array -vs-c-vector /

你有没有建议我如何做这项任务没有太多的工作(显然,因为代码已经写,只需要传送)?

Do you have a suggestion on how I might do the task without much work (clearly, because the code is already written, needs only be "transferred")?

推荐答案

您可以编写一个包装器库,使用,然后。在对象中调用方法的函数可能如下所示:

You can write a wrapper-library that uses internal handles to the objects and classes used, then. A function to call a method in an object might look like this:

extern "C" int classname_methodname(int handle, int param1);

然后,句柄 into a vector< classname>

The handle parameter can then be an index into a vector<classname> internal_classname.

在我上面的例子中,实际的函数 classname_methodname

The actual function, classname_methodname in my example above, just fetches the object from the vector and calls the method in the object with its paremeters.

要创建一个对象实例,你可以有一个 classname_create 或类似的命名函数,创建一个新实例,然后返回用于其他函数的句柄。

To create an object instance, you can have a classname_create or similar named function, that creates a new instance and then returns the handle used for the other functions.

这篇关于便于在现有C实现中使用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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