开发者

including header file

I have some question about header files(I'm talking about c, but I think it will be the same for c++), let's assume I have some my_ADT.c file (inside I have implementation of the functions and actual struct) and also my_ADT.h inside I have pointer for my struc开发者_Python百科t Question: if I use ADT Set for implementation my_ADT do I need to include set.h to both files my_ADT.h and my_ADT.c or including only to my_ADT.h will be sufficient (inside my_ADT.c I have #include "my_ADT.h") thanks in advance


There are 3 scenarios

  1. set.h is needed ONLY in my_ADT.h
  2. set.h is needed ONLY in my_ADT.c
  3. set.h is needed in both my_ADT.h and my_ADT.c
  4. set.h is not needed at all :-)

For scenario 3) add the #include "set.h" to the file my_ADT.h, document that fact, and #include "my_ADT.h" in my_ADT.c (with proper include guards, you lose nothing by including set.h also to the C file).

For scenario 2) include set.h only in my_ADT.c

For scenario 1) include set.h only in my_ADT.h


If my_ADT.h is included in the my_ADT.c file, then you should include set.h only in my_ADT.h.


In addiction to pmg answer you must know that a good way of avoiding #include problem is to surround all header file with a precomplier instruction that check if the .h file has been already included.

#ifndef __my_ADT_header
#define __my_ADT_header
/*
 * your header declaration here
 */
#endif

this prevent error caused by multiple header inclusion

hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜