Here are the major difference between C++ and Obj C:
- 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. 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
false, Objective-C uses
- C++ uses
NULL, Objective-C prefers
- 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
- 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. 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 can create structs on the stack, for instance.