简体   繁体   English

什么时候使用C作为面向对象的语言?

[英]When is it appropriate to use C as object oriented language?

There are a lot of excellent answers how can one simulate object oriented concepts with C. To name a few: 有很多优秀的答案如何用C模拟面向对象的概念。仅举几例:

When is it appropriate to use such simulation and not to use languages that support object-oriented techniques natively? 什么时候使用这种模拟是合适的,而不是本地使用支持面向对象技术的语言?

Highly related: 高度相关:

I'll give you the one reason I know of because it has been the case for me: 我会告诉你我知道的一个原因,因为我一直都是这样:

When you are developing software for a unique platform, and the only available compiler is a C compiler . 在为独特平台开发软件时,唯一可用的编译器是C编译器 This happens quite often in the world of embedded microcontrollers. 这在嵌入式微控制器领域经常发生。

To just give you another example: a fair amount of the x86 Linux kernel is using C as if it were C++, when object-orientation seems natural (eg, in the VFS). 再举几个例子:当面向对象看似自然时(例如,在VFS中),相当数量的x86 Linux内核正在使用C,就好像它是C ++一样。 The kernel is written in assembly and C (if that wasn't changed in the 3.0 kernel). 内核是用汇编和C编写的(如果在3.0内核中没有更改)。 The kernel coders create macros and structures, sometimes even named similar to C++ terms (eg, for_each_xxx), that allow them to code as-if. 内核编码器创建宏和结构,有时甚至命名类似于C ++术语(例如,for_each_xxx),允许它们按照如果进行编码。 As others have pointed out, you'd never choose C if you start a heavily object-oriented program; 正如其他人所指出的,如果你开始一个面向对象的重度项目,你永远不会选择C; but when you're adjusting C based code to add object-oriented features, you might. 但是,当您调整基于C的代码以添加面向对象的功能时,您可能会这样做。

When you want a cross-platform foundation for object-oriented APIs. 当您需要面向对象API的跨平台基础时。 A case in point is Apple's Core Foundation . 一个典型的例子是Apple的核心基金会 Being entirely C, it could be easily ported, yet provides an extremely rich set of opaque objects to use. 完全是C,它可以很容易移植,但提供了一组非常丰富的不透明对象。

A nice example of its flexibility is the way many of its types are 'toll-free' bridged with those from Foundation (a set of true OO Objective-C libraries). 它的灵活性的一个很好的例子是它的许多类型与来自Foundation(一组真正的OO Objective-C库)的那些“免费”桥接。 Many types from Core Foundation can be used, fairly naturally, in Foundation APIs, and vice-versa. Core Foundation中的许多类型可以很自然地用在Foundation API中,反之亦然。 It's hard to see this working so well without some OO concepts being present in the Core Foundation libraries. 如果Core Foundation库中没有一些OO概念,很难看到这种工作如此顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM