将C ++引用包装到C API中 [英] Wrapping C++ references into a C API
问题描述
我有一个头文件,应该是一个组合的C ++和C API。 C部分被包装到 externC
中,但声明本身看起来与C非常不同;例如:
foo :: Bar& Foo_Baz_GetBarOfQux(void * _self,const foo :: Qux& qux);
有没有办法实际使用它的形式?我显然不能使用这个头,但也许我可以重新声明它以某种方式:
/ *类型? * / Foo_Baz_GetBarOfQux(void *,const / * type?* /);什么困惑我是如何声明这些引用的类型(如果可能的话)。
。
我知道我可以在上面写我自己的C包装:我声明类作为不透明结构:
typedef struct foo_bar foo_bar_t ;
typedef struct foo_baz foo_baz_t;
typedef struct foo_qux foo_qux_t;
,然后写一个类似C的函数,它包含上述函数:
externC
foo_bar_t *
foo_baz_get_bar_of_qux(
foo_baz_t * baz,
foo_qux_t * qux)
{
return(foo_bar_t *)Foo_Baz_GetBarOfQux(baz,*(foo :: Qux *)qux);
}
但我不知道我是否可以使用原来的 不能使用 I have a header file for what is supposed to be a combined C++ and C API. The C part is wrapped into Is there a way to actually use it form plain C? I obviously cannot use this header, but maybe I can redeclare it somehow: What puzzles me is how to declare types of these references (if it's possible). P.S. I know I can write my own C wrapper on top of this: I declare the classes as opaque structures: and then write a C-like function that wraps the above function: But I wonder if I can use the original No you cannot use 这篇关于将C ++引用包装到C API中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Foo_Baz_GetBarOfQux
Foo_Baz_GetBar() / code>直接调用
c ++
中的成员函数涉及隐式传递 this
指针不存在于您的 c
代码中。extern "C"
, but the declarations themselves look very much unlike C; for example:foo::Bar& Foo_Baz_GetBarOfQux(void *_self, const foo::Qux &qux);
/* type? */ Foo_Baz_GetBarOfQux(void*, const /* type? */);
typedef struct foo_bar foo_bar_t;
typedef struct foo_baz foo_baz_t;
typedef struct foo_qux foo_qux_t;
extern "C"
foo_bar_t*
foo_baz_get_bar_of_qux(
foo_baz_t* baz,
foo_qux_t* qux)
{
return (foo_bar_t*) Foo_Baz_GetBarOfQux(baz, *(foo::Qux*)qux);
}
Foo_Baz_GetBarOfQux()
directly.Foo_Baz_GetBar()
directly, the calls to a member function in c++
involves implicitly passing of an this
pointer which does not exist in your c
code.