C++ vs Objective C

oc

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).
OC允许参数命名,方法的signature包含了名称,包括参数类型,返回类型。
而c++的成员函数signature包含函数名,和参数类型,返回类型,不包括名字
C++ uses bool, true and false, Objective-C uses BOOL, YES and NO.
bool的不同
C++ uses void* and nullptr, Objective-C prefers id and nil.
void*的不同
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.
messaging机制
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
可以发送消息给nil
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.
oc允许dynamic dispatch,允许class运行时对消息进行反映。而c++的方法必须在编译时确定
Objective-C allows autogeneration of accessors for member variables using "properties".
支持properties,实现队成员变量accessors的自动生成
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) foo and - (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 alloc message, 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 can create structs on the stack, for instance.

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++ syntax的语法更简单。而oc对dynamic dispatch的支持更好

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.

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.

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

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

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.
主要的不同是oc关注运行时的决策,用于dispatching,严重依赖于运行时库来处理inheritance and polymorphism。而c++关注静态,编译时的决策
Regarding libraries, you can use plain C libraries in both languages - but their native libraries are completely different.

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

They‘re completely different. Objective C has more in common with Smalltalk than with C++ (well, except for the syntax, really).
oc和smalltalk更相似,而不是c++

时间: 2024-10-17 16:01:50

C++ vs Objective C的相关文章

Qt for iOS,Qt 与Objective C混合编程

项目设置 既然要聊 Qt 混合 OC 编程,首先要简单介绍一下 Objective C .我只有一句话:Go,问搜索引擎去.因为我所知实在有限,怕误导了您.当然如果您不怕,往下看吧. OC源文件介绍 首先我要说一下 Objective C 的源文件,后缀是.m 或 .mm ,在 .mm 文件里,可以直接使用 C++ 代码.所以,我们要混合 Qt 代码与 OC 代码,就需要在 Qt 项目里加入 mm 文件. pro 文件配置 Qt SDK for Mac ,安装之后, Qt Creator 会使用

K-means: optimization objective(最小化cost function来求相应的参数)

类似于linear regression,K-means算法也optimization objective或者是试图使cost function求最小值. 了解K-means算法的optimization objective有助于我们(1)调试算法时,看算法是否运行正确(在本节中可以看到)(2)使算法找到更好的cluster,避免局部最优解(在下节中会讲) K-means optimization objective uc(i):表示x(i)分给的那个cluster的cluster centro

objective - c 类和对象

知识点 1.初步认识 OC 2.xcode使用 3.面向对象编程思想,类和对象 4.类的定义和使用 5.类的实例化--对象 6.声明和实现相分离 =========================================== 1.需要理解的(理论部分) 第一部分:OC和面向对象 1.1.OC语言的历史,特点.重要程度 [什么是OC] objective - c   在C语言基础上开发, 面向对象的语言: 注意C语言和oc语言语法的区别: IOS, iphone, ipad … [进入OC

Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

Qt/C++开发人员眼中的Obj-C 对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这篇博客的内容,我将描写叙述诸多我所学到的东西.这将很多其它的是大脑转存而不是单一的教程 ,所以我仍然希望这将对你非常有帮助,我将首先写编程语言的差异,然后再写关于类库的差异 1.Objective C vs C vs C++ 类似于C++,Obj-c是C语言的一个超集(这不是100%正确的,可是一个足够好的语

Objective - c Chapter 1 -2 Hello world

Objective - c   Chapter 1  Hello world 1.1 1.2.On the Welcome screen, click "Create a new Xcode project" (see Figure 2-1), or just chooseFile ->New ->New Project. 1.3.在main里写如下代码 #import <Foundation/Foundation.h> int main(int argc, c

Objective C笔记(第一天)

• OC语言概述 1.早在20世纪80年代早期,Bard Cox发明了Objective C, 是扩充的C,面向对象的编程语言. 2.NEXTSTEP简称NS a.1985年,Steve Jobs成?立了NeXT公司. b.1988年,NeXT SoftWare公司获得了OC的语言授权, NEXTSTEP环境成为了苹果操作系统,创建的开发包.MACOS c.1996年,12月20?日,苹果公司宣布收购了NeXT software公司,NEXTSTEP环境为apple公司下主要开发.发?行操作系统

Windows下搭建objective C开发环境

最近打算针对iPhone.iPod touch和iPad开发一些应用,所以,需要开始学习Objective C(苹果推出的类似C语言的开发语言).由于苹果的自我封闭的产业链发展模式(从芯片.机器.开发语言.终端产品.服务)的限制,要想开发针对苹果 iPhone等产品的应用程序,就需要用Mac机器,在Xcode的IDE上使用Objective C语言开发.Mac机器就不用说了,那比PC机器要贵不少(当然性价比还是很高的),所以,要想廉价方式学习Objective C就必须要在Windows上能搭建

Objective-C 源码(一):对象模型

首先,你要先弄清楚什么是类,什么是对象.(为了说清楚这个问题,先举个栗子:类->对象->实例:人类是类:某个人是对象:你是实例:实例本身也是对象.这下基本能理解其中的差别了,要是还是搞不清楚,自己去google一下吧.) 对象 在Objective-C中,每一个对象都是某个类的实例,且这个对象的isa指针指向它所属的类.打开 objc-private.h文件,在第126行可以看到 Objective-C???? 对象的定义struct objc_object ,Objective-C中对象本质

iOS开发——语法OC篇&amp;Objective-C新特性的总结

Objective-C新特性的总结 1.nonnull nonnull : 标示当前属性不为空,让外界放心用,只做标示用,即使为空,也木有办法    相当于swift里面的 ! 号 @property (nonnull, nonatomic, strong) NSString *name; 2.nullablenullable : 标示当前属性可能为空,让外界使用时注意    相当于swift里面的 ? 号 @property (nullable, nonatomic, strong) NSSt