这个问题很有意思,我之前还没碰到过呢,我帮你在全球最大的编程论坛stackoverflow上搜了一个答案:
这个答案大意是说,C语言没有this指针,所以要自己写一个wrap API来封装C++的对象。
这个论坛高手云集,包括很多业界大拿,所以这个答案还是很可信的。下面的api.h 就是你要写的wrap API
C has no thiscall notion. The C calling convention doesn't allow directly calling C++ object member functions.
Therefor, you need to supply a wrapper API around your C++ object, one that takes the this pointer explicitly, instead of implicitly.
Example:
// C.hpp
// uses C++ calling convention
class C {
public:
bool foo( int arg );
};
C wrapper API:
// api.h
// uses C calling convention
#ifdef __cplusplus
extern "C" {
#endif
void* C_Create();
void C_Destroy( void* thisC );
bool C_foo( void* thisC, int arg );
#ifdef __cplusplus
}
#endif
Your API would be implemented in C++:
#include "api.h"
#include "C.hpp"
void* C_Create() { return new C(); }
void C_Destroy( void* thisC ) {
delete static_cast(thisC);
}
bool C_foo( void* thisC, int arg ) {
return static_cast(thisC)->foo( arg );
}
转:
阅读(1530) | 评论(0) | 转发(0) |