“ Objective-C 是比 C + + 更严格的 C 的超集”到底是什么意思?

据我所知是 为什么 Objective-C 在苹果社区之外不是很受欢迎?

Objective-C 是 c 的超集(实际上比 c + + 严格得多) ,因此不会出现向下兼容问题。你可以在 C 中做任何事情,你也可以在 Objective-C 中做。

作为超集是二进制的,就像怀孕一样。 Obj-C 是 C 的超集,而 C + + 不是。

他们说的超集是什么意思?Objective-C 在哪些方面与 C 更接近//向后兼容?Objective-C 在哪些方面比 C + + 更接近于 C 哲学?

任何 C 程序都可以不经过目标 C 编译器(100% 兼容性)的修改而进行编译吗?

这更多的是一个关于编程语言设计和兼容性的问题,而不是一场关于哪种语言更好的战争。

9987 次浏览
  1. What do they mean by superset?

    They mean strict superset. Any valid C program will compile with an Objective-C compiler. Some valid C programs will not compile with a C++ compiler.

  2. In what way does objective-C would be more close//backward compatible to C?

    Here's a simple example:

    int *foo = malloc(12);
    

    Compiles in C and Objective-C, but not in C++. There are, of course, other examples as well.

  3. In what way does objective-C follow the C philosophy more closely than C++?

    All - Objective-C is a strict superset of C.

  4. Can any C program be compiled without modification by a objective-C compiler (100% compatibility)?

    Yes.

"Objective-C is a superset of C" means that every valid C program is a valid Objective-C program (with the same meaning).

It is sometimes said, although not by C++ experts, that C++ is a superset of C. This isn't accurate, which is why your quotation is making a big deal of comparing the two.

From the ground up, C++ has been designed as a "better C", fixing design omissions, both real and perceived, as the authors of C++ went through the language. The result of this design decision has been that X being a valid C program did not guarantee that X would compile, let alone run, when processed by the C++ compiler. The changes touched such basic constructs as string literals (they became const char*), assignment of void pointers, conversions between enums and integral types, semantics of compound assignment operators, and so on.

Moreover, once C99 came along, features that made it into the updated C standard were left out from the updated C++ standard. Again, very important language features were left out - most notably, designated initializers and variable-size arrays.

In contrast, Objective C has been positioned as a superset of C, requiring all valid C programs to be compilable with an Objective C compiler.

I prepared a simple diagram; it is not very pretty, but hopefully gets the point across:

  • Red: the set of all programs valid in C, C++, and Objective-C (relatively small)
  • Green: the set of all programs valid in C and Objective-C, but invalid in C++ (even smaller)
  • Gray: the set of all programs valid in Objective C and C++, but invalid in C (empty, as far as I know)
  • Blue: the set of all programs valid only in Objective C (relatively large)
  • Yellow: the set of all programs valid only in C++ (largest)

The set of valid C programs (in red and green) is an strict subset of the set of valid Objective C programs (blue)

enter image description here

Objective C is a set of backward-compatible extensions to C. This is possible because the Objective C features are delimited in two very simple ways:

  • use of the character @. This character is not currently used in the C language.
  • a simple syntactic extension for invoking methods, [obj method:argument]. In C, square brackets are used in a very specific way for array subscripting, and so this is invalid C syntax. Extensions which build on invalid syntax do not change the meaning of anything that is valid in the host language.

So easy to see that no program which uses Objective C extensions can be a strictly conforming ISO C program, no matter how simple. Moreover, every ISO C program can be declared, by definition, to be a valid Objective C program. Objective C can easily follow developments like C99 and C11.

On the other hand, C++ is not simply extensions to C; it is a different language which changes the meaning of some of the syntax of C. C++ and C are separately maintained, and so their relationship changes over time. For instance, C has acquired new features that are completely absent in C++, and quite likely will not go into C++, such as C99 variable-length arrays. C++ cannot easily pick up new C features.

If you write a portable C program, it should be at the same time an Objective C program. But additional care will be needed so that it is also a C++ program with the same meaning. (This practice is not unheard of, and the dialect it requires is informally known as "Clean C").

A trivial example of a C program that breaks when treated as C++ is any C program which uses a C++ keyword as an identifier, such as class or virtual. Objective C does not introduce any reserved keywords. It has new keywords that are introduced by the @ character, like @interface.