用C语言设计 [英] Design in C language

查看:87
本文介绍了用C语言设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是C ++/VC ++开发人员,我也了解C.对于使用C ++或C#进行设计,我们有一些类.我们可以在Class的基础上分离和抽象我们的逻辑.现在,没有像" C语言"这样的类了.我们必须仅出于所有设计目的而依赖功能.您能以任何方式提出建议或链接吗,以便我可以更好地用C语言设计软件.

感谢和问候

Hi All,
I am C++/VC++ developer, I know C as well. For design in C++ or C# we have classes. We can sepreate and abstract our logic on the basis of Class. Now there is nothing like class in ''C language''. We have to be dependant on functions only for all the design purpose. Can you please suggest any way or link to so I can design software in C language in better way.

Thanks and Regards

推荐答案

如果您知道C ++,我不明白为什么要使用C,但是问题本身很有趣.

事情是这样的:即使在汇编语言中,您也可以模拟或实现面向对象的编程风格.从语法的角度看,它看起来很丑陋,但是开发技术实际上是面向对象的,只是不像一种好的面向对象语言中的万无一失. >
您没有类,但是可以使用C结构(struct).您不能在结构内部定义方法,但并不是那么重要:您可以仅将单独的标头和单独的C文件用于在某种结构类型上工作的方法集.更重要且更像OOP的是:您的所有方法都应具有按引用结构或指向结构的指针类型的第一个参数.这将与称为"this"("self"是其他语言)的OOP 即时方法的不可见参数紧密匹配.

这是模拟封装的方法. 继承力的模拟将更加丑陋:作为一种选择,您应该实现一个模拟"extensions"类型的指针的容器.

您没有问这个问题,但是如何模拟OOP的本质-虚拟方法和后期绑定呢?您也可以做到!您将需要创建一个模拟虚拟方法表(虚拟表)的结构.您可以将其实现为指针列表或数组,并通过每个指针组织间接调用.这是比较困难的练习,但却是可行的.我会说,我只能告诉你,如果你能做到的话,你对C很熟悉. :-)

可以看到Microsoft软件如何自动模拟这种面向对象的C功能".使用用于为COM组件(不仅是COM)创建接口的IDL语言,请参阅 http://www.omg.org/gettingstarted/omg_idl.htm [ http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa367300%28v=vs.85%29.aspx [
I don''t understand why would you use C if you know C++, but the question itself is pretty interesting.

Here is the thing: you can simulate or implement object-oriented style of programming even in Assembly language. From the syntactic point of view, it would look pretty ugly, but the technology of development will be really object-oriented, just not so fool-proof as in a good object-oriented language.

You don''t have classes, but you can use C structures (struct). You cannot define the methods inside the structure, but it is not so important: you can just use a separate header and a separate C file for the set of methods working on a certain structure type. What is more important and more OOP-like: all your methods should have the very first parameter of the structure-by-reference or pointer-to-structure type. This will closely match the invisible parameter of the OOP instant method called "this" ("self" is some other languages).

This is the way you can simulate encapsulation. Simulation of inheritance will be much more ugly: as an option, you should implement a container of pointer of type "extensions", simulated.

You did not ask about it, but how about simulation of the very essence of OOP — virtual methods and late binding? You can do it, too! You will need to create a structure simulating virtual method table (virtual table). You can implement it as a list or array of pointers and organize indirect calls via each pointer. This is more difficult exercise but quite doable. I would say, I''ll only be able to tell you that you know C well if you can do it. :-)

There is one what to see how such object-oriented C "features" are simulated automatically by Microsoft software. Use the IDL language used to create interfaces for COM components (not just COM, please see http://msdn.microsoft.com/en-us/library/windows/desktop/aa367091%28v=vs.85%29.aspx[^] vs. http://www.omg.org/gettingstarted/omg_idl.htm[^]). Get some IDL code with interfaces or create your own and compile it into C code using MIDL compiler (http://msdn.microsoft.com/en-us/library/windows/desktop/aa367300%28v=vs.85%29.aspx[^]). Learn this auto-generated code — you will get a very good source of idea on how to implement OOP in C!

—SA


这篇关于用C语言设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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