Sample如下:
Here's a quick example of how you might go about wrapping a C++ class or API in a C-callable API that does little more than forward the C calls to the C++ object.
Suppose you have a C++ library with the following interface:
class stopwatch
{
public:
void start();
void stop();
void reset();
unsigned int get_elapsed();
private:
// whatever...
};
Your C API might have a interface that's described by the following header:
#ifndef STOPWATCH_API_H
#define STOPWATCH_API_H
#if __cplusplus
extern "C" {
#endif
struct stopwatch_handle;
stopwatch_handle* stopwatch_create(void);
void stopwatch_delete( struct stopwatch_handle*);
void stopwatch_start(struct stopwatch_handle*);
void stopwatch_stop(struct stopwatch_handle*);
void stopwatch_reset(struct stopwatch_handle*);
void stopwatch_get_elapsed(struct stopwatch_handle*);
#if __cplusplus
}
#endif
#endif
Note that the above he

本文详细介绍了如何在C语言中封装C++类,通过创建C-callable API,实现在C环境中使用C++库的功能。涉及了C接口定义、C++类接口、内存管理以及异常处理等关键步骤。

1310

被折叠的 条评论
为什么被折叠?



