打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C 函数中调用 C ++函数2

// cpp.h
#ifndef  __cpp_h__
#define  __cpp_h__

class  class1 {
    class1();
    ~class1();
public:
    int  I;
    int  J;

    int  getI(void);
};

#endif
// end file

// cpp.cpp
#i nclude "stdafx.h"
#i nclude  <iostream>
#i nclude  "cpp.h"
#i nclude  "c.h"

using namespace  std;       // 打开标准库名字空间

class1::class1()
{}

class1::~class1()
{}

int  class1::getI(void)
{
    return  I++;
}

// 按 C 调用方式编译下面函数
extern "C"
int  get_class1_I(struct1 * p)
{
    class1 * pClass1 = (class1 *)p;

    cout << "c++: " << pClass1->getI() << endl;

    return  pClass1->getI();
}

// end file

// c.h
#ifndef  __c_h__
#define  __c_h__

#ifdef  __cplusplus
extern "C" {
#endif

    typedef struct {
        int  i;             // 与 class1 类中变量一致
  int  j;
    }struct1;

#ifdef  __cplusplus
}
#endif
#endif
// end file

// c.c
#i nclude  <cstdio>
#i nclude  "c.h"

extern  int  get_class1_I(void * p);

struct1  s;

int  main(void)
{
    printf ("c: %d\n", get_class1_I(&s));
    printf ("c: %d\n", get_class1_I(&s));
 
    return 0;
}

// end file

参考了eCos中的混和编程实现方式(http://sources.redhat.com/ecos/)。

本例在ADS 1.2中编译通过,执行结果正确。
VC++中编译时,C.C文件编译选项中选择 Not using precompile headers。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
STL运用的C++技术(6)——函数对象
CERT C++编码规范翻译(EXP) | MathxH
详解C结构体、C++结构体 和 C++类的区别
extern使用方法总结!
C++ 对象的Lua脚本化 第2页
memset与memcpy函数使用详细说明
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服