本文介绍了如何从C-文件调用C ++构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导入这个程序,它解析了很多复杂的文本,它的用C写的我自己的项目用C ++编写。

I've imported this program, which is parsing a lot of complex text and it's written in C. My own project is written in C++.

其实,我打算,每当解析器算法已经找到了钥匙牌之一,那么一个(很多)我类的构造函数应该被调用,所以我会在结尾部分有一个很好的结构TXT,作为解析过程的结果。

I actually intended, that whenever the parser-algorithm has found one of the key-tags then a one (of many) constructor of my class should be called, so that I'll have a nice structure in the end of the txt, as a result of the parsing process.

这里的问题:我学到面向对象与Java,并开始C ++这个项目,所以我需要一点点的帮助:我怎么能调用C ++构造出我的C基于解析器的文件?我已经在网上查过,但无论是这个问题太琐碎,或我的意图的解决方案是不工作;)

Here's the problem: I learned OOP with Java and started C++ with this project, so I need a little help: how can I call a C++ constructor out of my C based parser file ? I already checked the internet, but either this question is too trivial or my intended solution is not working ;)

感谢您的任何建议。

推荐答案

您不能调用构造函数,直接,但你可以创建一个工厂分配的功能和返回的对象的实例,你可以写这些功能,使得定义在C ++(如果是可以用新来分配对象,并使用C ++的构造函数),但可调用从C提供的。

You can't invoke the constructor, directly, but you can create factory functions that allocate and return instances of your object, and you can write these functions such that the definition is provided in C++ (where it is possible to use "new" to allocate the object and use the C++ constructors) but callable from C.

在头,你应该写:

 #ifdef __cplusplus
 #  define EXTERNC extern "C"
 #  define NOTHROW noexcept
 #else
 #  define EXTERNC
 #  define NOTHROW
 #endif

 /* Alias for your object in C that hides the implementation */
 typedef void* mylibraryname_mytype_t;

 /* Creates the object using the first constructor */
 EXTERNC mylibraryname_mytype_t mylibraryname_create_mytype() NOTHROW;

 /* Creates the object using the second constructor */
 EXTERNC mylibraryname_mytype_t mylibraryname_create_mytype_with_int(int val) NOTHROW;

 /* Frees the object, using delete */
 EXTERNC void mylibraryname_free_mytype(mylibraryname_mytype_t obj) NOTHROW;

然后在你的C ++源文件,你可以这样做:

Then in your C++ source file, you can do:

 EXTERNC mylibraryname_mytype_t mylibraryname_create_mytype() NOTHROW {
   try {
     return static_cast<mylibraryname_mtype_t>(new MyType);
   }
   catch (...) {
     return nullptr;
   }
 }

 EXTERNC mylibraryname_mytype_t create_mytype_with_int(int val) NOTHROW {
   try {
     return static_cast<mylibraryname_mytype_t>(new MyType(val));
   }
   catch (...) {
       return nullptr;
   }
 }

 EXTERNC void mylibraryname_free_mytype(mylibraryname_mytype_t obj) NOTHROW {
   try {
     MyType* typed_obj = static_cast<MyType*>(obj);
     delete typed_obj;
   }
   catch (...) {
       // Ignore
   }
 }

然后

您C code应该能够包括相同的标题,当对生成的库链接使用来自C ++源文件中的定义。

Your C code should then be able to include the same header and use the definition from the C++ source file when linked against the generated library.

注意,code以上吞咽异常批发。对于一个真正的API,你应该(例如,由通过输出参数分配的对象返回,并返回一个状态code),而不是仅仅燮pressing他们提供指示错误给调用者的一种方式。

Note that the code above is swallowing exceptions wholesale. For a real API, you should provide a way of indicating errors to the caller (e.g. by returning the allocated object via an output parameter and returning a status code) rather than merely suppressing them.

修改

正如在评论中指出,_t在技术上是一个保留的后缀(虽然你应该罚款,如果你的符号有preFIX这是不太可能由标准库在未来使用),所以只要确保你的符号包括库名称为preFIX。还应当指出的是,typedef的,尽管不是必需的,是为了使对象更自我说明比原始无效*所有的地方。

Edit
As noted in the comments, "_t" is technically a reserved suffix (though you should be fine if your symbols have a prefix that is unlikely to be used by standard libraries in the future), so just make sure that your symbols include the library name as a prefix. It should also be noted that the typedef, though not required, is intended to make uses of the object more self-documenting than a raw "void*" all over the place.

这篇关于如何从C-文件调用C ++构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:32