C语言 C语言是面向对象的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3241932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is the C programming language object-oriented?
提问by Brian T Hannan
I was talking with a co-worker about C and C++ and he claimed that C is object-oriented, but I claimed that it was not. I know that you can do object-oriented-like things in C, but C++ is a true object-oriented language.
我和一位同事谈论 C 和 C++,他声称 C 是面向对象的,但我声称它不是。我知道你可以用 C 做类似面向对象的事情,但 C++ 是一种真正的面向对象语言。
What are your thoughts?
你怎么看?
Also, it triggered discussion on who decides what it means to be object-oriented and that it's tough to say what object-oriented really officially means. What are you thoughts on this?
此外,它引发了关于谁决定面向对象意味着什么的讨论,并且很难正式说出面向对象的真正含义。你对此有何想法?
回答by James McNellis
If by "is C object oriented?" you mean "is C designed with facilities specifically to support object oriented programming?" then, no, C is clearly not object oriented.
如果通过“C 是面向对象的吗?” 您的意思是“C 设计的设施是否专门用于支持面向对象的编程?” 那么,不,C 显然不是面向对象的。
回答by Tim Robinson
You can program in an object-orientated style in more or less any language. (I think runtime polymorphism -- i.e. virtual methods -- requires a language that supports function pointers.)
您可以使用或多或少的任何语言以面向对象的风格进行编程。(我认为运行时多态性——即虚拟方法——需要一种支持函数指针的语言。)
Here are a couple of examples:
下面是几个例子:
- A short summary of object-orientated style in C: http://www.emilmont.net/doku.php?id=c:object_oriented_c
- A comparison between the same program written in C and C++: http://www.eventhelix.com/realtimemantra/basics/object_oriented_programming_in_c.htm
- C 语言中面向对象风格的简短总结:http: //www.emilmont.net/doku.php?id=c: object_orient_c
- 用 C 和 C++ 编写的相同程序的比较:http: //www.eventhelix.com/realtimemantra/basics/object_orienting_programming_in_c.htm
回答by NotMe
C isn't object oriented. That was the entire purpose behind the ++
C 不是面向对象的。这就是 ++ 背后的全部目的
As far as a definition of what it takes to be object oriented: check wikipedia.
至于什么是面向对象的定义:检查wikipedia。
Personally, if it supports inheritance, encapsulation, and polymorphism then your good to go. Another key here is having nice keywords like class and object tend to help...
就个人而言,如果它支持继承、封装和多态,那么你就可以了。这里的另一个关键是使用类和对象之类的好关键字往往会有所帮助......
Examples of real object oriented languages (not conclusive) are: Smalltalk, Java, c#, Python, Ruby, C++..
真正的面向对象语言(非决定性)的例子是:Smalltalk、Java、c#、Python、Ruby、C++..
Also, it's possible to have extensions to provide OO features like PHP, Perl, VB (not .Net), ...
此外,还可以通过扩展来提供 OO 功能,例如 PHP、Perl、VB(不是 .Net)等。
回答by Roddy
Real programmers can write object-oriented code in ANY language.
真正的程序员可以用任何语言编写面向对象的代码。
But no, C is not an 'object-oriented' language. It has no concept of classes, objects, polymorphism, inheritance.
但是不,C 不是“面向对象”语言。它没有类、对象、多态、继承的概念。
回答by bzimage
Answer can be yes or no, depending on:
答案可以是 yes 或 no,具体取决于:
if you ask "is C an object oriented language?", the answer is "no" because it do not have object oriented constructors, keywords, semantic etc...
if you intend "can I realize OOP in C?", the answer is yes, because OOP is not only a requirement of a language, but also a way of "thinking", an approach to programming, before to touch some language or another. However the implementation of OOP in C (or any other language not natively designed to be OOP) will be surely "forced" and much hard to manage then any other OOP language, so also some limitation shall be expected.
如果你问“C 是一种面向对象的语言吗?” ,答案是否定的,因为它没有面向对象的构造函数、关键字、语义等......
如果您打算“我可以在 C 中实现 OOP 吗?” ,答案是肯定的,因为OOP不仅仅是一种语言的要求,也是一种“思考”的方式,一种编程的方法,在接触某种语言之前。然而,在 C(或任何其他并非本机设计为 OOP 的语言)中实现 OOP 肯定会“强制”并且比任何其他 OOP 语言更难以管理,因此也应该有一些限制。
回答by Bill Torcaso
C is not an O-O language under any definition of "O-O" and "language".
在“OO”和“语言”的任何定义下,C 不是 OO 语言。
It is quite easy to use C as the implementation language for a component that gives an O-O API to its clients. The X Windows system is essentially a single-inheritance O-O system when viewed from its API, but a whole mess of C when viewing its implementation.
使用 C 作为组件的实现语言非常容易,该组件为其客户端提供 OO API。从其 API 来看,X Windows 系统本质上是一个单继承 OO 系统,但从其实现来看,则是一堆 C 语言。
回答by ttchong
- C is not object oriented in strict sense since it doesn't have a built-in syntax supported object oriented capability like class, inheritance and so on.
- C 不是严格意义上的面向对象,因为它没有内置语法支持的面向对象功能,如类、继承等。
But if you know the trick you can easily add object oriented capability to it simply using struct, function pointer, & self-pointer. DirectFB is such a C library written in an object oriented way. The bad thing it is more error prone since it is not governed by syntax and compile type checking. It is based on coding convention instead.
但是如果你知道这个技巧,你可以简单地使用结构、函数指针和自指针轻松地向它添加面向对象的功能。DirectFB 就是这样一个以面向对象的方式编写的 C 库。不好的是它更容易出错,因为它不受语法和编译类型检查的约束。它基于编码约定。
e.g.
例如
IDirectFB/*a typedef of a struct*/ *dfb = NULL;
IDirectFBSurface/*another typedef of a struct*/ *primary = NULL;
DirectFBCreate (&dfb); /*factory method to create a struct (e.g. dfb) with
pointers to function and data. This struct is
like an object/instance of a class in a language with build-in
syntax support for object oriented capability */
dfb->SetCooperativeLevel/*function pointer*/
(dfb/*self pointer to the object dfb*/,
DFSCL_FULLSCREEN);
dsc.flags = DSDESC_CAPS;
dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
dfb->CreateSurface/*function pointer, also a factory method
to create another object/instance */
( dfb/*self pointer to the object dfb*/,
&dsc,
&primary/*another struct work as object of another class created*/ );
primary->GetSize/*function pointer*/
(primary/*self pointer to the object primary*/,
&screen_width,
&screen_height);
2 . C++ is object oriented since it has built-in support for object oriented capability like class and inheritance. But there is argument that it is not a full or pure object oriented language since it does allow C syntax (structural programming syntax) in it. I also remember that C++ lack a few object oriented capabilities but not remember each one exactly.
2 . C++ 是面向对象的,因为它内置了对类和继承等面向对象功能的支持。但是有人认为它不是完整的或纯粹的面向对象语言,因为它确实允许使用 C 语法(结构化编程语法)。我还记得 C++ 缺乏一些面向对象的功能,但不能准确地记住每一个。
回答by Graphics Noob
The confusion may be that C can be used to implement object oriented concepts like polymorphism, encapsulation, etc. which may lead your friend to believe that C is object oriented. The problem is that to be considered an object oriented programming language, these features would need to be built into the language. Which they are not.
混淆可能是 C 可用于实现面向对象的概念,如多态性、封装等,这可能会让您的朋友相信 C 是面向对象的。问题是要被视为面向对象的编程语言,这些功能需要内置到语言中。他们不是。
回答by Timo Geusch
Unless your friend was talking about Objective C (an OO superset of C) then no, C isn't an OO language. You can implement OO concepts using C (that's what the old cfront C++ compiler did, it translated C++ into C) but that doesn't make C an OO language as it doesn't specifically offer support for standard OO techniques like polymorphism or encapsulation.
除非您的朋友在谈论 Objective C(C 的 OO 超集),否则不,C 不是 OO 语言。您可以使用 C 实现 OO 概念(这是旧的 cfront C++ 编译器所做的,它将 C++ 翻译成 C),但这并不使 C 成为一种 OO 语言,因为它没有专门提供对标准 OO 技术(如多态性或封装)的支持。
Yes, you can write software OO style in C, especially with liberal (ab-)use of macros but as someone who has seen the results of some of those attempts, I'd strongly suggest to use a better suited language.
是的,您可以用 C 编写 OO 风格的软件,尤其是在自由(ab-)使用宏的情况下,但作为已经看到其中一些尝试结果的人,我强烈建议使用更适合的语言。
回答by newbie007
Real programmers can write object-oriented code in ANY language.
真正的程序员可以用任何语言编写面向对象的代码。
I haveseen Object Oriented Cobol. Cobol that calls Cobol. Do you want to call these programmers "Real"?
我已经看到面向对象的Cobol。称为 Cobol 的 Cobol。你想称这些程序员为“真实的”吗?

