views:

683

answers:

8

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

*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.

+13  A: 

This guide gives the best comparison I've seen.

LiraNuna
+3  A: 

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

Dean Harding
+9  A: 

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.

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++.

Georg Fritzsche
+20  A: 

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, true and false, Objective-C uses BOOL, YES and NO.
  • C++ uses void* and NULL, Objective-C prefers id and 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 NULL
  • 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) 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.

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.

Mac
Decent list; one correction. They aren't "named parameters", but "interleaved parameters". Named and "keyword arguments" lead to confusion of thinking that some subset of the method name may be omitted. It cannot.
bbum
You forgot to enlist the most important difference: Object-C uses dynamic dispatch, whereas C++ uses static dispatch. In other words, code compiled by an Objective-C compiler will have the class responsible for responding to a message determined at runtime; code compiled by a C++ compiler have this information calculated and compiled-in at compiletime.
wilhelmtell
@bbum, wilhelmtell: Excellent points both - I've updated my answer to reflect your comments.
Mac
Just a slight correction to the second point: A method's name in Objective-C is called its *selector* — the method *signature* refers to the types the method takes and returns (see, for example, the NSMethodSignature class).
Chuck
@wilhelmtell: The C++ compiler knows only the superclass at compile time. At run time the actual class could be any descendant. This is also a form of dynamic dispatch, but not the same form as is used in Objective C. Just be careful with those technical terms!
Norman Ramsey
+1 Good list. However, Objective-C also uses `void*` and `NULL`, just not for objects. You can use any C-style pointer in Obj-C, and many API calls actually pass or return values by reference, in which case `NULL` is frequently used.
Quinn Taylor
@wilhelmtell - I dunno anything about objective-C, but in C++ you CAN dynamically have a different class respond to a function call, but you'd need to have something like an array of pointers to a base class, and then the ACTUAL classes that are "hanging" off of it. While all classes there need to be sub-classes, a method call WILL call different methods depending on the class, at run-time.
Kevin
In Objective-C, you can create a class at runtime. There are also no namespaces in Objective-C.
dreamlax
+2  A: 

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).

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.

Each have their strengths.

Rev316
+1  A: 

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

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

C++ will not compile unless the void pointer is explicitly cast:

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

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

Igor Zevaka
+1  A: 

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

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.

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).

Michael E
A: 

I want to thank you all for contributing and answering my question. Also, I want to give credit to two answers in particular.

Thank you!

Alerty