免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3304 | 回复: 4
打印 上一主题 下一主题

请教C++调用C库的关键字冲突 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-17 18:39 |只看该作者 |倒序浏览
请问C++调用C库是,遇到关键字冲突怎么处理?

比如Xlib中window的属性中有一个class字段


       typedef struct {
            int x, y;                /* location of window */
            int width, height;       /* width and height of window */
            int border_width;        /* border width of window */
            int depth;               /* depth of window */
            Visual *visual;          /* the associated visual structure */
            Window root;             /* root of screen containing window */
            int class;               /* InputOutput, InputOnly*/            int bit_gravity;         /* one of the bit gravity values */
            int win_gravity;         /* one of the window gravity values */
            int backing_store;       /* NotUseful, WhenMapped, Always */
            unsigned long backing_planes;/* planes to be preserved if possible */
            unsigned long backing_pixel;/* value to be used when restoring planes */
            Bool save_under;         /* boolean, should bits under be saved? */
            Colormap colormap;       /* color map to be associated with window */
            Bool map_installed;      /* boolean, is color map currently installed*/
            int map_state;           /* IsUnmapped, IsUnviewable, IsViewable */
            long all_event_masks;    /* set of events all people have interest in*/
            long your_event_mask;    /* my event mask */
            long do_not_propagate_mask;/* set of events that should not propagate */
            Bool override_redirect;  /* boolean value for override-redirect */
            Screen *screen;          /* back pointer to correct screen */
       } XWindowAttributes;

class字段会导致编译不过,如何处理呢?

论坛徽章:
1
午马
日期:2013-08-23 23:39:47
2 [报告]
发表于 2010-04-17 18:46 |只看该作者
那个头里undef行不行

论坛徽章:
0
3 [报告]
发表于 2010-04-17 19:00 |只看该作者
那个头里undef行不行
phy0077 发表于 2010-04-17 18:46



    关键字不是define出来的

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
4 [报告]
发表于 2010-04-17 19:08 |只看该作者
悲剧……   linux内核代码也是这样……   用new作参数名……

你看这样行不行?

  1. ------ x.h ------

  2. typedef struct {
  3.       int class;
  4. } X;

  5. void insert( ..., X* new);
复制代码
既然x.h包含了class、 new等关键字, 那它肯定没打算用于C++编译器。
那么:

  1. ------ xpp.h ------

  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif

  5. #define class class_
  6. #define new new_

  7. /** 将 x.h中的C++关键字换一个名字
  8. 因为x.h没打算用于C++编译器, 所以替换了也是合法代码
  9. */
  10. #include "x.h"

  11. /** 取消, 使得余下部分不受影响 */
  12. #undef class
  13. #undef new

  14. #ifdef __cplusplus
  15. }
  16. #endif
复制代码
对函数参数, 比如:

  1. void insert( ..., X* new);
复制代码
被替换成:

  1. void insert( ..., X* new_);
复制代码
不会造成什么影响。

对结构体嘛……  就看你用不用那个域了……
如果要直接使用, 而不是交给API去处理, 余下部分的代码就只好这样:

  1. ------ x.cpp ------

  2. #include "xpp.h"
  3. void f() {
  4.       X x;
  5.       x.class_ = 1212;
  6. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-04-17 19:57 |只看该作者
悲剧……   linux内核代码也是这样……   用new作参数名……

你看这样行不行?既然x.h包含了class、 new ...
OwnWaterloo 发表于 2010-04-17 19:08



    看来也只有如此了。
   我原以为这个问题普遍存在,有什么编译器的选项可以用呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP