我只需要在C中编写几次,似乎每次我做,它变成一些难以管理的野兽。
I've only had to code in C a few times, and it seems like every time I do, it becomes some unmanageable beast.
我已经完成我的大部分编程在C#和.Net中,所以我非常习惯于类风格的架构,但我似乎无法掌握在C应用程序中的组织。我应该把与某个文件相关的功能放在一个文件中,将功能定义放在一个文件中,另一个处理在另一个文件中。在这个问题上有没有什么好的资源?
I've done most of my programming in C# and .Net so I am very accustomed to the class style architecture, but I can't seem to grasp organization in C applications. Should I put functions that are related in a certain file, put function definitions in one file and processing in another, etc. Are there any good resources out there on this subject?
另请参见:与面向对象的土地挣扎着C?
推荐答案以下是可能有助于您的类比:
Here is an analogy that might help you:
一个类是某种数据结构和一组访问和操作该数据的函数。 c 语言不提供任何合成糖来为您组织此关系,但您仍然可以围绕这个想法构建代码。所以,只要这样做:设计你围绕一堆课程进行设计。
A class is a data structure of some kind and a set of functions to access and manipulate that data. The c language doesn't provide any syntatic sugar to organize this relationship for you, but you can still build your code around this idea. So, just do it: design you project around a bunch of "classes".
代码组织通常采用两种形式之一:
Code organization generally takes on one of two forms:
- 每个函数都得到一个头文件(接口)文件,一个 .h 扩展名和一个 .c extension
- 每个类(即相关函数集)都会获得一个头文件和一个实现文件。
- Each function gets one header (interface) file with a .h extension and one implementation file with a .c extension
- Each "class" (i.e. set of related functions) gets one header file and one implementation file.
所以,你已经知道如何做...
So, you already know how to do this...
更多推荐
在C中计划和组织应用程序的最佳方法是什么?
发布评论