C + + vs Objective C

Source: Internet
Author: User

Oc

Short list of some of the major differences:

C + + allows multiple inheritance, objective-c doesn ' t.
One that allows multiple inheritance, one that does not allow
Unlike C + +, Objective-c allows method parameters to being 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/retu RN (without their names).
The OC allows the parameter to be named, and the signature of the method contains the name, including the parameter type, return type.
The C + + member function signature contains the function name, and the parameter type, return type, not including the name
C + + uses bool, true and FALSE, Objective-c uses bool, YES and NO.
Different of BOOL
C + + uses void* and nullptr, Objective-c prefers ID and nil.
The difference between void*
Objective-c uses "selectors" (which has the type SEL) as an approximate equivalent to function pointers.
??
Objective-c uses a messaging paradigm (a la Smalltalk) where can send "messages" to objects through methods/selectors.
Messaging mechanism
Objective-c would happily let's send a message to nil, unlike C + + which would crash if you try to call a member function o F nullptr
Can send a message to nil
OBJECTIVE-C allows for dynamic dispatch, allowing the class responding to a message to being determined at runtime, unlike c+ + Where the object A method is invoked upon must was known at compile time (see Wilhelmtell ' s comment below). This was related to the previous point.
OC allows dynamic dispatch to allow the class runtime to reflect the message. The C + + method must be determined at compile time
Objective-c allows autogeneration of accessors for member variables using "Properties".
Supports properties to automate generation of team member variable accessors
OBJECTIVE-C allows assigning to self, and allows class initialisers (similar to constructors) to return a completely diffe Rent 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 being of the type you originally specified.
Similarly, in objective-c other classes could 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's, 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 was due to Objective-c's named parameters being functionally equivalent to C + + ' s name mangling.
Objective-c would happily allow a method and a variable to share the same name, unlike C + + which would typically have fits. I Imagine this was something to does with the objective-c using selectors instead of function pointers, and thus method names not Actually has a "value".
Objective-c doesn ' t allow objects to being created on the Stack-all objects must is allocated from the heap (either Explici tly with a alloc message, or implicitly in an appropriate factory method).
Like C + +, OBJECTIVE-C has both structs and classes. However, where in C + + they is treated as almost exactly the same, in objective-c they is treated wildly differently-yo u 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.
I think the biggest difference is the grammar, I think C + + syntax syntax is simpler. And OC's support for dynamic dispatch is better

Probably plenty of other things too that I ' ve missed, I'll update with any other things I think of. Other than this, can highly recommend the guide Liranuna pointed. Incidentally, another site of interest might is this.

I should also point out that I ' m just starting learning objective-c myself, and as such a lot of the above is not quite B e correct or Complete-i apologise if that's the case, and welcome suggestions for improvement.

Edit:updated to address the points raised on the following comments, added a few more items to the list.


While they is both rooted in C, they is, completely different languages.

A major difference is this 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.
The main difference is that OC focuses on runtime decisions for dispatching and relies heavily on the runtime library to handle inheritance and polymorphism. While C + + focuses on static, compile-time decisions
Regarding libraries, you can use plain C libraries in both languages-but their native libraries is completely different .

Of interest though is so 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 and Smalltalk are more similar than C + +

C + + vs Objective C

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.