Explanation of differences between Objective-C and C ++

Source: Internet
Author: User

Objective-CAndC ++The difference is the content to be completed in this article. It is easier for us to learn through the distinction between the two. Let's just look at the details.

1. The biggest difference between the two is that they are all object-oriented languages evolved from C and both are compatible with the standard C language.

2. The biggest difference between the two is that Objective C provides a dynamic binding mechanism during runtime, while C ++ is static binding during compilation and embedding classes (Multi-inheritance) and virtual functions (Virtual tables) to simulate implementation. objective C supports dynamic message forwarding at the language level. Its function call syntax is [object function], and C ++ is object-> function (). the meanings of the two are also different,

In Objective C, it refers to sending a message to an object. As for whether the object can process the message (that is, whether the function is implemented) and whether to process or forward the message, it will not crash, in C ++, an operation is performed on an object. If the object does not have this operation, an error is reported during compilation (static binding ), or the program will crash (dynamic binding ).

3. Objective C does not support multiple inheritance, whereas C ++ does. however, Objective C can more elegantly implement this feature through proxy and Category. this also includes the differences between overwriting and overloading.

4. function name resolution is also different. Objective C's function name resolution is based on the function name + parameter name, while C ++ only includes the function name. therefore, the following are valid in Objective C, because the two are parsed to be similar to foo: bar1 AND foo: bar2, respectively;

-(Int) foo :( int) bar1; // the original author "Objective C's function name resolution is a function name + Parameter Name". Here the parameter name actually refers to "parameter description ", only two or more parameters can have this meaning. The author directly describes the name of the parameter value as a parameter, and it is incorrect to describe this concept.

 
 
  1. -(char*)foo:(int) bar2; 

(Correct, the original explanation for this is wrong, there is no overload in Objective-c, the O-C only recognizes the function name including the parameter description ))

To implement a function similar to overload, you must first install the parameter with NSNumber or NSValue and then use it as a parameter. This is stupid and intelligent.

In C ++, the following statements are invalid. compilation errors occur because the two statements are similar to foo: int, which cannot be identified by the compiler.

 
 
  1. int foo(int bar1);  
  2. char* foo(int bar2); 

5. Objective-C does not have any constructor or destructor in C ++. Its counterpart is alloc-init/free.

6. Objective-C 2.0 is added to the garbage collection mechanism (unavailable on iPhone and iPad), while C ++ does not.

7. Objective-C does not allow memory allocation on the Stack. It can only be performed on heap, while C ++ supports both.

8. Objective-C does not support references, but C ++ does.

9. Objective-C does not support templates, but C ++ does.

10. Objective-C is a Smalltalk system, while C ++ Is A Simula system.

In this case, Objective-C is simply packaged on top of C, while C ++ is complicated on top of C ++, but Objective-C is more elegant and concise.

Summary: DetailsObjective-CAndC ++I hope this article will help you with the difference!

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.