将C ++引用包装到C API中 [英] Wrapping C++ references into a C API

查看:131
本文介绍了将C ++引用包装到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);
}

但我不知道我是否可以使用原来的 Foo_Baz_GetBarOfQux

不能使用 Foo_Baz_GetBar() / code>直接调用 c ++ 中的成员函数涉及隐式传递 this 指针不存在于您的 c 代码中。


I have a header file for what is supposed to be a combined C++ and C API. The C part is wrapped into extern "C", but the declarations themselves look very much unlike C; for example:

foo::Bar& Foo_Baz_GetBarOfQux(void *_self, const foo::Qux &qux);

Is there a way to actually use it form plain C? I obviously cannot use this header, but maybe I can redeclare it somehow:

/* type? */ Foo_Baz_GetBarOfQux(void*, const /* type? */);

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:

typedef struct foo_bar foo_bar_t;
typedef struct foo_baz foo_baz_t;
typedef struct foo_qux foo_qux_t;

and then write a C-like function that wraps the above function:

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

But I wonder if I can use the original Foo_Baz_GetBarOfQux() directly.

解决方案

No you cannot use 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.

这篇关于将C ++引用包装到C API中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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