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++
C++ vs Objective C的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- Objective C类方法load和initialize的区别
Objective C类方法load和initialize的区别 过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...
随机推荐
- 201621123001 《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 通过IP地址和端口号可以进行建立Socket对象,然后进行通信 使用Socket的一般步骤: 建立Sock ...
- winform datatable 或datagridview中添加列
DataGridViewCheckBoxColumn dg = new DataGridViewCheckBoxColumn(); dg.HeaderText = "选择"; dg ...
- jQuery中extend()实现原理
jQuery.extend使用的几种方式 1.jQuery.extend(源对象) jQuery源代码: if(length == i){ target = this; --i; } 示例1: var ...
- 【Python】基础练习题-1
#练习1:从键盘输入两个数,并比较其大小,直到输入e/E退出程序 while 1: input_number=raw_inut("please input two numbers,enter ...
- idea【取消多行】
有时间把idea总结一下 idea打开很多文件时默认收起来就很烦. 这样可以取消多行 效果大概是这样 .酥服哒.
- [转]How rival bots battled their way to poker supremacy
How rival bots battled their way to poker supremacy http://www.nature.com/news/how-rival-bots-battle ...
- php操作mysql几个常用操作
1.链接数据库 mysql_connet('数据库地址','数据库账号','数据库密码'); 2.选择数据库 mysql_select_db("数据库名"); 3.设置编码 mys ...
- Linux关闭透明大页配置
一.为何要关闭透明大页 A--MOS获取 . #翻译 由于透明超大页面已知会导致意外的节点重新启动并导致RAC出现性能问题,因此Oracle强烈建议禁用透明超大页面. 另外,即使在单实例数据库环境 ...
- 【leetcode】7-ReverseInteger
problem: Reverse Integer 注意考虑是否越界: INT_MAX INT_MIN 32bits or 64bits 调整策略,先从简单的问题开始:
- [LeetCode&Python] Problem 706. Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...