如何有效地构建一个C程序 [英] How to structure a C program effectively

查看:148
本文介绍了如何有效地构建一个C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先说,我已经得到了在C和C ++经验相当数量。不过,我开始用C一个新项目,我在面向对象的语言已经工作了这么长时间(C#和C ++),我有想出的程序语言来封装功能的有效途径麻烦。我首先想到的是简单地回到属于我的OO知识创新和体制它是这样的:

Let me first say that I've got a fair amount of experience in both C and C++. However, I'm starting a new project in C and I've been working in object-oriented languages for so long (C# and C++) that I am having trouble coming up with an effective way to encapsulate functionality in a procedural language. My first thought was to simply fall back on my OO knowledge and structure it something like:

struct Foo 
{
    int x;
    char *y;
}; 

struct Foo *new_Foo()
{
    return (struct Foo *)malloc(sizeof(struct Foo));
} 

void Foo_member_function(struct Foo *foo, int z)
{
    foo->x = z;    
}

但是,这似乎只是单调乏味的,违反了C的精神,更何况,它是一种穷人的OO。

But that just seems tedious and contrary to the spirit of C. Not to mention that it is a poor-man's OO.

此程序最终会得到相当可观的,所以从一个好的设计组织起非常关键。我想随着年龄的在C开发的,某些设计模式在发达国家如何最好地结构中的code可维护性。就像函数式编程,我希望过程编程有一个范例,是干净的,相当可读。

This program is ultimately going to get fairly sizable, so starting from a good design organization is critical. I imagine with the years of development in C, certain design patterns have developed in how to best structure the code for maintainability. Much like functional programming, I'm hoping that procedural programming has a paradigm that is clean and fairly readable.

指针相关的文章和书籍是可以接受的为好。

Pointers to relevant articles and books are acceptable as well.

推荐答案

这是一个相当正常的,合理的做法。但尽量不要暴露好头文件的结构布局,让你在它是如何实现的一些灵活性和管理您的依赖关系。

This is quite a normal and sensible practice. But try not to expose the struct layout in header files, so that you have some flexibility in how it's implemented and manage your dependencies better.

请参阅不透明指针了解更多详情。

这篇关于如何有效地构建一个C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆