Objective C vs C++

Objective C Pros:

  • dynamic dispatch(run time) and c++ use static dispatch

  • extend a class with a category, c++ only can use multi inheritance

  • All the function are virtual function

  • selector instead of function pointer

  • Memory management:ARC

  • Syntax: smalltalk , more readable

  • you can send a message to nil, C++ will crash if you try to call a member function of nullptr

  • auto generation of accessors for member variables using "properties".

Objective C Cons:

  • C++ allows multiple inheritance

  • no name space

  • no operator overloading

  • you can only allocate objects from the heap

stack对象通常有速度的优势,而且不会发生内存泄露问题。那么为什么OC的对象都是分配在heap的呢? 原因在于:

stack对象的生命周期所导致的问题。例如一旦函数返回,则所在的stack frame

就会被摧毁。那么此时返回的对象也会一并摧毁。这个时候我们去retain这个对

象是无效的。因为整个stack frame都已经被摧毁了。简单而言,就是stack对象

的生命周期不适合Objective-C的引用计数内存管理方法。

stack对象不够灵活,不具备足够的扩展性。创建时长度已经是固定的,而stack对象

的拥有者也就是所在的stack frame

results matching ""

    No results matching ""