c/c++ 前置声明 -- typedef问题

本文探讨了C++中前置声明的应用及限制,特别是针对typedef结构体的情况,并提供了有效的实现方法。

前置声明的好处很多, 比如能避免头文件互相包含的冲突, 比如有时我们在一个头文件中只需要另一个头文件的某个类型定义, 只需要对它做一下前置声明即可,

  因为为了相对较小的目的要包含进来一个很大的头文件, 实在有些"不值", 况且这个头文件可能还要被其它很多文件再包含的, 这样代价就更大了.

  所以做前置声明即可, 在源文件真正需要的时候再包含那个头文件.

  之前也声明过类和c++方式定义的struct, 今天遇到了一个typedef的结构体, 结果前置声明不奏效了.

  网上查了些资料, 有的帖子没有讨论出结果, 有的直接定性typedef的不能前置声明, 只能include进来.

  幸好自己写小例子多测试了一会, 发现语法上还是支持的. 举例如下:

  #a.h:

  [cpp]  typedef struct my_time_t

  {

  int hour, minute, second;

  } MY_TIME;

  typedef struct my_time_t

  {

  int hour, minute, second;

  } MY_TIME;

  #b.h

  [cpp]  struct my_time_t;

  typedef struct my_time_t MY_TIME;

  void func(MY_TIME* mt) {}

  struct my_time_t;

  typedef struct my_time_t MY_TIME;

  void func(MY_TIME* mt) {}

  #main.cpp

  [cpp]  #include "a.h"

  #include "b.h"

  int main()

  {

  MY_TIME mt;

  func(&mt);

  return 0;

  }

  #include "a.h"

  #include "b.h"

  int main()

  {

  MY_TIME mt;

  func(&mt);

  return 0;

  }

  这样就可以成功了. 在b.h中做前置声明时, 先声明有my_time_t这样一个struct, 然后说明MY_TIME是由那个结构体typedef出来的,

  这样void func(MY_TIME* mt);这个函数声明就能编译通过了.   直接做struct MY_TIME;这样的前置声明是不被接受的.

[root@nino-ai-db make-4.3]# make Making all in lib make[1]: Entering directory `/root/glibc-2.28/build/make-4.3/lib' make all-recursive make[2]: Entering directory `/root/glibc-2.28/build/make-4.3/lib' make[3]: Entering directory `/root/glibc-2.28/build/make-4.3/lib' make[3]: Nothing to be done for `all-am'. make[3]: Leaving directory `/root/glibc-2.28/build/make-4.3/lib' make[2]: Leaving directory `/root/glibc-2.28/build/make-4.3/lib' make[1]: Leaving directory `/root/glibc-2.28/build/make-4.3/lib' Making all in po make[1]: Entering directory `/root/glibc-2.28/build/make-4.3/po' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/root/glibc-2.28/build/make-4.3/po' Making all in doc make[1]: Entering directory `/root/glibc-2.28/build/make-4.3/doc' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/root/glibc-2.28/build/make-4.3/doc' make[1]: Entering directory `/root/glibc-2.28/build/make-4.3' depbase=`echo src/ar.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\ gcc -DHAVE_CONFIG_H -Isrc -I./src -Ilib -I./lib -DLIBDIR=\"/usr/local/make/lib\" -DINCLUDEDIR=\"/usr/local/make/include\" -DLOCALEDIR=\"/usr/local/make/share/locale\" -g -O2 -MT src/ar.o -MD -MP -MF $depbase.Tpo -c -o src/ar.o src/ar.c &&\ mv -f $depbase.Tpo $depbase.Po In file included from src/ar.c:22:0: src/filedef.h:123:49: error: use of enum ‘cmd_state’ without previous declaration void set_command_state (struct file *file, enum cmd_state state); ^ In file included from src/ar.c:23:0: src/dep.h:67:10: error: declaration of ‘floc goaldep::floc’ [-fpermissive] floc floc; ^ In file included from src/ar.c:18:0: src/makeint.h:488:5: error: changes meaning of ‘floc’ from ‘typedef struct floc floc’ [-fpermissive] } floc; ^ In file included from src/ar.c:23:0: src/dep.h:133:6: error: use of enum ‘update_status’ without previous declaration enum update_status update_goal_chain (struct goaldep *goals); ^
最新发布
04-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值