简体   繁体   English

面向对象的概念是否适用于C语言?

[英]Are object-oriented concepts applicable to the C language?

In C#, we learned that function overloading occurs when more than one function have the same identifier but different signature. 在C#中,我们了解到,当多个函数具有相同的标识符但签名不同时,就会发生函数重载。

Although the concept of function overloading is specific to object-oriented languages, is it applicable to the C language as well on the basis of the following observation? 尽管函数重载的概念特定于面向对象的语言,但基于以下观察,它是否也适用于C语言?

printf("%d", 3);

printf("%d + %d = %d", 1 , 2 , 3 );

The first printf passes only TWO arguments. 第一个printf仅传递两个参数。 The second printf passes FOUR arguments. 第二个printf传递四个参数。

Does that mean printf is overloaded? 这是否意味着printf超载?

No printf is not overloaded. 没有printf不过载。 There is no function overloading or any other object oriented goodness in C. The way printf works is by using argument list. C中没有函数重载或任何其他面向对象的优点。printf的工作方式是使用参数列表。 Take a look at this article http://www.cprogramming.com/tutorial/c/lesson17.html 看看这篇文章http://www.cprogramming.com/tutorial/c/lesson17.html

The printf family of functions does not use overloading, but rather take a variable number of arguments. printf函数系列不使用重载,而是采用可变数量的参数。

Function overloading and overriding (ie virtual methods in C#) are not supported in C, which doesn't even have member functions. C中不支持函数重载和重写(即C#中的虚拟方法),C中甚至没有成员函数。

However they can be imitated by means of function pointers. 但是,可以通过函数指针来模仿它们。 This is the approach taken for instance in the implementation of the X Window System. 这是在X窗口系统的实现中采用的方法。

Read about variadic functions . 了解variadic functions

printf and scanf family of functions are variadic functions. printfscanf系列函数是可变函数。

Useful Links: 有用的链接:

  1. Cprogramming.com Cprogramming.com

  2. Writing Variadic Functions 编写可变参数函数

To complete the answer: C does not support Function Overloading. 要完成答案,请执行以下操作:C不支持函数重载。

No printf is not an example for function overloading as others already stated, it uses the features from stdarg.h . 没有printf并不是函数重载的示例,正​​如其他人已经指出的那样,它使用了stdarg.h的功能。 But seemingly unknown to many, C has some sorts of function overloading. 但是对于许多人来说似乎并不为人所知,C具有某种函数重载。

  • since C99 C has "type generic mathematical functions" in tgmath.h that have you eg compute float or double sin depending on the argument that you are passing 由于C99 C在tgmath.h中具有“类型通用数学函数”,因此您需要根据所传递的参数来计算floatdouble sin
  • since C11 has _Generic , a feature that is even more powerful than function overloading, and that can be used, among other things, to write function-like macros that implement overloading features 因为C11具有_Generic ,所以此功能比函数重载甚至更强大,并且可以用来编写实现重载功能的类似于函数的宏

And, all of this has nothing to do with OO programming. 而且,所有这些都与OO编程无关。

Function overloading is language feature completely separate from object-oriented design. 函数重载是与面向对象设计完全独立的语言功能。 Just because it tends to exist in languages that have OO features, doesn't make it an OO feature as well. 仅仅因为它倾向于存在于具有OO功能的语言中,所以也不能使其成为OO功能。

The corner stones of OO design are: autonomous classes with limited dependences towards the outside world ("loose coupling"), private encapsulation of data/methods and inheritance/polymorphism. OO设计的基石是:对外部世界的依赖性有限的自治类(“松散耦合”),数据/方法的私有封装以及继承/多态性。

Every other feature that doesn't fit in with the above is just some extra fluff. 上面不适合的所有其他功能只是一些额外的功能。 Function overloading and operator overloading are two such fluffy things - there are many OO languages that don't support those 2 features. 函数重载和运算符重载就是两个这样的笨拙的事情-许多OO语言不支持这两个功能。

As for printf , it uses the icky variable argument feature of the C language. 至于printf ,它使用C语言的icky变量参数功能。 I wouldn't call that function overloading, but rather some old ad hoc crap that was introduced in the 70s just so that C could brag about having variable number of arguments. 我不会称其为函数重载,而是称其为70年代引入的一些旧的临时废话,以便C可以吹嘘具有可变数量的参数。 A feature which is mildy useful, to say the least. 至少可以说,此功能适度有用。 It isn't used in any sane, production-quality code. 任何理智的,生产质量的代码中都不会使用它。

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

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