organizing code files in C & C++
来源:互联网 发布:ip网络终端 编辑:程序博客网 时间:2024/05/22 20:08
http://www.gamedev.net/reference/programming/features/orgfiles/page2.asp
通过四个pitfall讲解头文件的由来和好处,以及使用头文件定义inline func and template.
Remember that, as far as the compiler is concerned, there is absolutely no difference between a header file and a source file.
The key idea is that headers contain the interface, and the source files contain the actual implementation.This means that one source file uses another source file via the second source file's header.
main body
By now you're probably convinced that there are benefits to splitting up your project into several smaller files. So, how would you go about it? Although some of the decisions you make will be reasonably arbitrary, there are some basic rules that you should follow to ensure that it all works.
Firstly, look at how you would split your code into sections. Often this is by splitting it into separate subsystems, or 'modules', such as sound, music, graphics, file handling, etc. Create new files with meaningful filenames so that you know at a glance what kind of code is in them. Then move all the code that belongs to that module into that file. Sometimes you don't have clear module - some would say this should send out warnings about the quality of your design! There may still be other criteria you use for splitting up code, such as the structures it operates upon. (Often "general purpose" functions can be split into string-handling and number-handling, for example.) And occasionally a module could be split into two or more files, because it might make sense to do so on logical grounds.
Once you have split it up in this way into separate source files, the next stage is to consider what will go into the header files. On a very simple level, code that you usually put at the top of the source file is a prime candidate for moving into a separate header file. This is presumably why they got termed 'header' files, after all.
This code to go in a header usually includes some or all of the following:
- class and struct definitions
- typedefs
- function prototypes
- global variables (but see below)
- constants
- #defined macros
- #pragma directives
(Additionally, when using C++, templates and inline functions usually need to be in the header file. The reasons for this should become clear later.)
...
Conclusion
If done properly, organizing your files this way can speed your development without hindering you at all or introducing any extra problems. Although it seems like there is a lot to consider when doing so, it becomes second nature in no time. Just remember these key points:
- Split your code up along logical module divisions
- The the interface into the header files and the implementation in the source files
- Use forward declarations wherever possible to reduce dependencies
- Add an inclusion guard to every header file you make
- Don't put code in the headers that might adversely affect source files that include it
Good luck!
- organizing code files in C & C++
- Files IO in C
- compaq C include head files in openvms
- Using C object files in Delphi
- Using C code in symbian
- CRC Implementation Code in C
- What is “:-!!” in C code?
- CRC Implementation Code in C
- C2059 Error Using extern "C" in .C Source Files
- C/S projects in Code Project Site
- Writing endian-independent code in C
- C Plus Plus in SCIM source code
- Packet Sniffer Code in C using sockets
- Using CSharp (C#) code in Powershell scripts
- Let us learn C in Code <0>
- Let us learn C in Code <1>
- Let us learn C in Code <2>
- Let us learn C in Code <10>
- 计算机专业课程 视频教程
- test
- jdk环境变量的配置
- Windows XP SP3系统安装IIS故障排除大全
- Tip - Windows 7 XP Mode不再需要硬件虚拟化支持
- organizing code files in C & C++
- 发送的法律所,了,
- 王海挺学设计模式-原型模式
- 奇怪的GPS接收数据现象
- linq to sql 分页 的几种方法
- 获得php所对应的配置文件(php.ini)信息
- 交换机,路由器,GPON等宽带产品,急招聘开发人员!!
- OpenGL中逆变换和模拟变换gluUnProject
- 动态内表 动态ALV显示