What is the difference between Objective-C from C++
Also Read : What is Reference Count in Objective-C’s Memory Management.
Also Read : What is Delegate Means in Objective-C (IOS Application Development)
C++
- Allows multiple inheritance
- 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,
- C++ uses void* and NULL,
- It will crash if you try to call a member function of NULL
- C++ where the object a method is invoked upon must be known at compile time
- 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.
- C++ allow implicit method overloading, That is, in C++ int foo (void) and int foo (int) define an implicit overload of the method foo.
- where in C++ they are treated as almost exactly the same
Objective-C
- It doesn’t allows multiple inheritance
- Allows method parameters to be named and the method signature includes only the names and types of the parameters and return type .
- Uses BOOL, YES and NO.
- Prefers id and nil.
- Uses “selectors” (which have type SEL) as an approximate equivalent to function pointers.
- Uses a messaging paradigm (a la Smalltalk) where you can send “messages” to objects through methods/selectors.
- It will happily let you send a message to nil,
- Allows for dynamic dispatch, allowing the class responding to a message to be determined at runtime
- Allows autogeneration of accessors for member variables using “properties”.
- Allows assigning to self, and allows class initialisers (similar to constructors) to return a completely different class if desired.
- Other classes may also dynamically alter a target class at runtime to intercept method calls.
- Lacks the namespace feature of C++.
- Lacks an equivalent to C++ references.
- Lacks templates, preferring (for example) to instead allow weak typing in containers.
- Doesn’t allow implicit method overloading , 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.
- It will happily allow a method and a variable to share the same name
- It 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.
- in Objective-C they are treated wildly differently – you can create structs on the stack, for instance.