Objective-C 与 C++ 有何不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445050/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:30:51  来源:igfitidea点击:

How different is Objective-C from C++?

c++objective-c

提问by Alerty

What are the main differences between Objective-C and C++ in terms of the syntax, features, paradigms, frameworks and libraries?

Objective-C 和 C++ 在语法、特性、范式、框架和库方面的主要区别是什么?

*Important: My goal is not to start a performance war between the two languages. I only want real hard facts. In fact, my question is not related to performance! Please give sources for anything that may seem subjective.

*重要提示:我的目标不是在两种语言之间展开一场性能大战。我只想要真实的事实。事实上,我的问题与性能无关!请提供任何看似主观的信息的来源。

回答by Mac

Short list of some of the major differences:

一些主要差异的简短列表:

  • C++ allows multiple inheritance, Objective-C doesn't.
  • Unlike C++, Objective-C allows method parameters to be named and the method signature includes only the names and types of the parameters and return type (see bbum's and Chuck's comments below). In comparison, a C++ member function signature contains the function name as well as just the types of the parameters/return (without their names).
  • C++ uses bool, trueand false, Objective-C uses BOOL, YESand NO.
  • C++ uses void*and nullptr, Objective-C prefers idand nil.
  • Objective-C uses "selectors" (which have type SEL) as an approximate equivalent to function pointers.
  • Objective-C uses a messaging paradigm (a la Smalltalk) where you can send "messages" to objects through methods/selectors.
  • Objective-C will happily let you send a message to nil, unlike C++ which will crash if you try to call a member function of nullptr
  • Objective-C allows for dynamic dispatch, allowing the class responding to a message to be determined at runtime, unlike C++ where the object a method is invoked upon must be known at compile time (see wilhelmtell's comment below). This is related to the previous point.
  • Objective-C allows autogeneration of accessors for member variables using "properties".
  • Objective-C allows assigning to self, and allows class initialisers (similar to constructors) to return a completely different class if desired. Contrast to C++, where if you create a new instance of a class (either implicitly on the stack, or explicitly through new) it is guaranteed to be of the type you originally specified.
  • Similarly, in Objective-C other classes may also dynamically alter a target class at runtime to intercept method calls.
  • Objective-C lacks the namespace feature of C++.
  • Objective-C lacks an equivalent to C++ references.
  • Objective-C lacks templates, preferring (for example) to instead allow weak typing in containers.
  • Objective-C doesn't allow implicit method overloading, but C++ does. That is, in C++ int foo (void)and int foo (int)define an implicit overload of the method foo, but to achieve the same in Objective-C requires the explicit overloads - (int) fooand - (int) foo:(int) intParam. This is due to Objective-C's named parameters being functionally equivalent to C++'s name mangling.
  • Objective-C will happily allow a method and a variable to share the same name, unlike C++ which will typically have fits. I imagine this is something to do with Objective-C using selectors instead of function pointers, and thus method names not actually having a "value".
  • Objective-C doesn't allow objects to be created on the stack - all objects must be allocated from the heap (either explicitly with an allocmessage, or implicitly in an appropriate factory method).
  • Like C++, Objective-C has both structs and classes. However, where in C++ they are treated as almost exactly the same, in Objective-C they are treated wildly differently - you cancreate structs on the stack, for instance.
  • C++ 允许多重继承,Objective-C 不允许。
  • 与 C++ 不同,Objective-C 允许命名方法参数,并且方法签名仅包括参数的名称和类型以及返回类型(请参阅下面的 bbum 和 Chuck 的评论)。相比之下,C++ 成员函数签名包含函数名称以及参数/返回的类型(没有它们的名称)。
  • C++ 使用bool,truefalse,Objective-C 使用BOOL,YESNO
  • C++ 使用void*and nullptr,Objective-C 更喜欢idand nil
  • Objective-C 使用“选择器”(具有 type SEL)作为近似等价于函数指针。
  • Objective-C 使用消息传递范式(a la Smalltalk),您可以在其中通过方法/选择器向对象发送“消息”。
  • Objective-C 会很乐意让你向 发送消息nil,不像 C++ 如果你尝试调用 的成员函数会崩溃nullptr
  • Objective-C 允许动态调度,允许在运行时确定响应消息的类,这与 C++ 不同,在 C++ 中调用方法的对象必须在编译时知道(请参阅下面的 wilhelmtell 评论)。这与上一点有关。
  • Objective-C 允许使用“属性”自动生成成员变量的访问器。
  • 如果需要,Objective-C 允许分配给self,并允许类初始化器(类似于构造函数)返回一个完全不同的类。与 C++ 不同的是,如果您创建一个类的新实例(隐式在堆栈上,或显式通过new),它保证是您最初指定的类型。
  • 同样,在 Objective-C 中,其他类也可以在运行时动态更改目标类以拦截方法调用。
  • Objective-C 缺少 C++ 的命名空间特性。
  • Objective-C 缺少等效于 C++ 的引用。
  • Objective-C 缺乏模板,更喜欢(例如)在容器中允许弱类型。
  • Objective-C 不允许隐式方法重载,但 C++ 允许。即,在C ++int foo (void)int foo (int)定义该方法的一个隐式过载foo,但要达到相同的目标C需要明确的重载- (int) foo- (int) foo:(int) intParam。这是因为 Objective-C 的命名参数在功能上等同于 C++ 的名称修饰。
  • Objective-C 很乐意允许一个方法和一个变量共享相同的名称,这与 C++ 不同,后者通常具有适合性。我想这与使用选择器而不是函数指针的 Objective-C 有关,因此方法名称实际上没有“值”。
  • Objective-C 不允许在堆栈上创建对象——所有对象都必须从堆中分配(显式地使用alloc消息,或隐式地在适当的工厂方法中)。
  • 和 C++ 一样,Objective-C 也有结构体和类。然而,在 C++ 中,它们被视为几乎完全相同,而在 Objective-C 中,它们被完全不同地对待——例如,您可以在堆栈上创建结构。

In my opinion, probably the biggest difference is the syntax. You can achieve essentially the same things in either language, but in my opinion the C++ syntax is simpler while some of Objective-C's features make certain tasks (such as GUI design) easier thanks to dynamic dispatch.

在我看来,最大的区别可能是语法。您可以用任何一种语言实现基本相同的东西,但在我看来,C++ 语法更简单,而由于动态调度,Objective-C 的某些功能使某些任务(例如 GUI 设计)更容易。

Probably plenty of other things too that I've missed, I'll update with any other things I think of. Other than that, can highly recommend the guide LiraNuna pointed you to. Incidentally, another site of interest might be this.

可能还有很多我错过的东西,我会更新我想到的任何其他东西。除此之外,强烈推荐 LiraNuna 指给您的指南。顺便说一下,另一个感兴趣的站点可能是this

I should also point out that I'm just starting learning Objective-C myself, and as such a lot of the above may not quite be correct or complete - I apologise if that's the case, and welcome suggestions for improvement.

我还应该指出,我自己才刚刚开始学习 Objective-C,因此上述很多内容可能并不完全正确或完整 - 如果是这种情况,我深表歉意,并欢迎提出改进建议。

EDIT: updated to address the points raised in the following comments, added a few more items to the list.

编辑:更新以解决以下评论中提出的要点,在列表中添加了更多项目。

回答by Georg Fritzsche

While they are both rooted in C, they are two completely different languages.

虽然它们都源于 C,但它们是两种完全不同的语言。

A major difference is that Objective-C is focused on runtime-decisions for dispatching and heavily depends on its runtime library to handle inheritance and polymorphism, while in C++ the focus usually lies on static, compile time, decisions.

一个主要区别是,Objective-C 专注于调度的运行时决策,并且严重依赖其运行时库来处理继承和多态性,而在 C++ 中,重点通常在于静态、编译时决策。

Regarding libraries, you can use plain C libraries in both languages - but their native libraries are completely different.

关于库,您可以使用两种语言的普通 C 库 - 但它们的本机库完全不同。

Of interest though is that you can mix both languages (with some limitations). The result is called Objective-C++.

有趣的是,您可以混合使用两种语言(有一些限制)。结果称为Objective-C++

回答by Dean Harding

They're completely different. Objective C has more in common with Smalltalk than with C++ (well, except for the syntax, really).

他们完全不同。与 C++ 相比,Objective C 与 Smalltalk 的共同点更多(嗯,除了语法,真的)。

回答by Rev316

Off the top of my head:

在我的头顶:

  1. Styles - Obj-C is dynamic, C++ is typically static
  2. Although they are both OOP, I'm certain the solutions would be different.
  3. Different object model (C++ is restricted by its compile-time type system).
  1. 样式 - Obj-C 是动态的,C++ 通常是静态的
  2. 尽管它们都是 OOP,但我确信解决方案会有所不同。
  3. 不同的对象模型(C++ 受其编译时类型系统的限制)。

To me, the biggest difference is the model system. Obj-C lets you do messaging and introspection, but C++ has the ever-so-powerful templates.

对我来说,最大的区别是模型系统。Obj-C 允许您进行消息传递和自省,但 C++ 拥有非常强大的模板。

Each have their strengths.

各有各的长处。

回答by Michael Ekstrand

As others have said, Objective-C is much more dynamic in terms of how it thinks of objects vs. C++'s fairly static realm.

正如其他人所说,与 C++ 相当静态的领域相比,Objective-C 在如何看待对象方面更具动态性。

Objective-C, being in the Smalltalk lineage of object-oriented languages, has a concept of objects that is very similar to that of Java, Python, and other "standard", non-C++ object-oriented languages. Lots of dynamic dispatch, no operator overloading, send messages around.

Objective-C 属于面向对象语言的 Smalltalk 谱系,其对象概念与 Java、Python 和其他“标准”非 C++ 面向对象语言的对象概念非常相似。大量动态调度,没有运算符重载,到处发送消息。

C++ is its own weird animal; it mostly skipped the Smalltalk portion of the family tree. In some ways, it has a good module system with support for inheritance that happens to be able to be used for object-oriented programming. Things are much more static (overridable methods are not the default, for example).

C++ 是它自己的怪异动物;它主要跳过了家谱中的 Smalltalk 部分。在某些方面,它有一个很好的模块系统,支持继承,恰好能够用于面向对象的编程。事情更加静态(例如,可覆盖的方法不是默认值)。

回答by Igor Zevaka

Objective-C is a more perfect superset of C. In C and Objective-C implicit casting from void*to a struct pointer is allowed.

Objective-C 是 C 的更完美的超集。在 C 和 Objective-Cvoid*中,允许隐式转换为结构指针。

Foo* bar = malloc(sizeof(Foo));

C++ will not compile unless the voidpointer is explicitly cast:

除非void显式转换指针,否则 C++ 不会编译:

Foo* bar = (Foo*)malloc(sizeof(Foo));

The relevance of this to every day programming is zero, just a fun trivia fact.

这与日常编程的相关性为零,只是一个有趣的琐事事实。

回答by Paul Fultz II

Obj-C has much more dynamic capabilities in the language itself, whereas C++ is more focused on compile-time capabilities with some dynamic capabilities.

Obj-C 在语言本身中具有更多的动态功能,而 C++ 更侧重于具有一些动态功能的编译时功能。

In, C++ parametric polymorphism is checked at compile-time, whereas in Obj-C, parametric polymorphism is achieved through dynamic dispatch and is not checked at compile-time.

在 C++ 中,参数多态在编译时检查,而在 Obj-C 中,参数多态是通过动态调度实现的,在编译时不检查。

Obj-C is very dynamic in nature. You can add methods to a class during run-time. Also, it has introspection at run-time to look at classes. In C++, the definition of class can't change, and all introspection must be done at compile-time. Although, the dynamic nature of Obj-C could be achieved in C++ using a map of functions(or something like that), it is still more verbose than in Obj-C.

Obj-C 本质上是非常动态的。您可以在运行时向类添加方法。此外,它还在运行时进行自省以查看类。在 C++ 中,类的定义不能改变,所有的内省都必须在编译时完成。尽管可以在 C++ 中使用函数映射(或类似的东西)来实现 Obj-C 的动态特性,但它仍然比 Obj-C 更冗长。

In C++, there is a lot more checks that can be done at compile time. For example, using a variant type(like a union) the compiler can enforce that all cases are written or handled. So you don't forget about handling the edge cases of a problem. However, all these checks come at a price when compiling. Obj-C is much faster at compiling than C++.

在 C++ 中,有更多的检查可以在编译时完成。例如,使用变体类型(如联合),编译器可以强制写入或处理所有情况。所以你不会忘记处理问题的边缘情况。但是,所有这些检查在编译时都是有代价的。Obj-C 的编译速度比 C++ 快得多。