从C调用C ++类方法 [英] Calling C++ class method from C

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

问题描述

大家好,


好​​的,这是问题/包版广告。我试图调用一个c ++类的成员的方法。我知道我需要一个包装器函数来给C指向成员函数的指针。我创建了一个共享头文件< header.h>和2个源文件。 < cSource.c>和< cppSource.cpp>。然后当然用c写的主文件。 < cMain.c>


如果有人能帮助我让这个小例子正常工作,以便我可以试验实际问题,我试图克服它将是非常可怕的。


这是我的代码:

(编译的命令和帖子末尾的错误)


header.h

展开 | 选择 | Wrap | 行号

解决方案

首先,cClass是一个C ++类,C中没有类,所以你可以在main()中创建一个cClass对象。


你可以c来自C的所有C ++函数,但你必须确保所有C ++函数都在C ++源文件中并编译为C ++。


从C调用的任何C ++函数都必须 externC来避开C ++ mangler。因此,你也失去了功能开发。


所以,从C中调用cClass :: sayHello你会在C main()中调用:


1)调用cSpeak()。没有参数。

这将是一个外部的C。 C ++函数。

2)cSpeak将在堆上创建一个cClass对象,并在该对象上调用sayHello。

3)cSpeak将删除cClass对象并返回

@weaknessforcats


哦我想我应该提到我想要做的包装物。从我的互联网旅程中我发现了一种方法,通过创建一个返回指向类方法的指针的包装函数。这是我找到的链接。它的第三句话是自然......。我有点理解发生了什么,但是我对某些部分的去处感到困惑。

http://www.research.att.com/~bs/bs_faq2.html#callCpp


你想在这里小心。


这是你引用的文章:


extern" C" double call_C_f(C * p,int i)//包装函数

{

返回p-> f(i);

}



这个函数接受一个C *,并使用该指针调用成员函数。

这正是我在我说的做的较早的帖子,除了没有C *参数:


现在C :: f()可以像这样使用:

/ * C代码:* /


double call_C_f(struct C * p,int i);


void ccc(struct C * p ,int i)

{

double d = call_C_f(p,i);

/ * ... * /

}



这里有一个假设:即结构C与C类具有相同的地址。它基于类的事实在C ++中实际上是作为结构实现的。就个人而言,我会通过在C函数和C ++端使用struct C *来使用struct C而不是C类来删除这个假设。


如果指定public / private / protected对于每个struct成员,然后struct和class是相同的。


我不知道这个假设是C ++标准的一部分还是依赖于编译器。每次我这样做,类和结构在C ++中都是一样的。


但是我编码了这个,ccc()看起来像这样:

展开 | < span class =codeLinkonclick =selectAll(this);>选择 | Wrap | 行号


Hello guys,

Ok here is the issue/roadblock. I am trying to call a method that is a member of a c++ class. I know I need to have a wrapper function to give C a pointer to the member function. I have created a shared header file <header.h> and 2 source files. <cSource.c> and <cppSource.cpp>. Then of course the main file written in c. <cMain.c>

If anyone could help me get this small example working so that I can experiment with the actual problem I''m trying to overcome it would be absolutely terrific.

Here is the code I have:
(the commands to compile and errors located at the end of post)

header.h

Expand|Select|Wrap|Line Numbers

解决方案

First, cClass is a C++ class and there are no classes in C so you can''t create a cClass object in main().

You can call any C++ functions from C but you will have to be sure all of the C++ functions are in C++ source files and compiled as C++.

Any C++ functions called from C have to be extern "C" to surn off the C++ mangler. Therefore, you lose function oveloading, too.

So, to call cClass::sayHello from C you would in the C main():

1) call cSpeak(). No arguments.
This would be an extern "C" C++ function.
2) cSpeak would create a cClass object on the heap and call sayHello on that object.
3) cSpeak would delete the cClass object and return


@weaknessforcats
Oh i guess I should have mentioned about the wrapper thing I was looking to do. From my internet journey I have discovered a way to do it, by creating a wrapper function that returns a pointer to the class method. Here is the link i found. Its about the third sentence down "Naturally...." I somewhat understand what is going on, however I''m confused about where certain pieces go.

http://www.research.att.com/~bs/bs_faq2.html#callCpp


You want to be careful here.

This is from your referenced article:

extern "C" double call_C_f(C* p, int i) // wrapper function
{
return p->f(i);
}

This function takes a C* and the member function is called using that pointer.
This is exactly what I said to do in my earlier post except without the C* argument:

Now C::f() can be used like this:
/* C code: */

double call_C_f(struct C* p, int i);

void ccc(struct C* p, int i)
{
double d = call_C_f(p,i);
/* ... */
}

Here an assumption is made: Namely the struct C has the same address as a class C. It''s based on the fact that classes in C++ are really implemented as structs. Personally, I would remove the assumption by using a struct C* in the C function and on the C++ side using a struct C rather than a class C.

If you specify public/private/protected for every struct member, then a struct and a class are identical.

I so not know if this assumption is part of the C++ standard or whether it is compiler-dependent. Every time I have done this the class and the struct work the same in C++.

But were I coding this, ccc() would look like this:

Expand|Select|Wrap|Line Numbers


这篇关于从C调用C ++类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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