A summary of C ++ Language Learning

Source: Internet
Author: User

For users and scholars who first came into contact with the C ++ language, it is very important to understand the concept of the C ++ language. let's first talk about what the C ++ language is, C ++ is a widely used computer programming language.

As a language to be compatible with C, C ++ retains the features of some procedural languages, which are known as "incomplete Ground Objects "), therefore, it can define global variables and functions that do not belong to any class. However, C ++ is an object-oriented programming language after all. To support function overloading, C ++ treats global functions differently from C.

Why do standard header files have a structure similar to the following?

 
 
  1. #ifndef __INCvxWorksh  
  2. #define __INCvxWorksh   
  3. #ifdef __cplusplus  
  4. extern "C" {  
  5. #endif   
  6. /*...*/   
  7. #ifdef __cplusplus  
  8. }  
  9. #endif   
  10. #endif /* __INCvxWorksh */ 

So:

 
 
  1. #ifdef __cplusplus  
  2. extern "C" {  
  3.  #endif   
  4.  #ifdef __cplusplus  
  5. }  
  6. #endif   


In the header file of the C language, the external function can only be specified as the extern type. The C language does not support the extern "C" declaration. when the c file contains extern "C", a compilation syntax error occurs.

 
 
  1. /* C header file: cExample. h */
  2. # Ifndef C_EXAMPLE_H
  3. # Define C_EXAMPLE_H
  4. Extern int add (int x, int y );
  5. # Endif
  6. /* C language implementation file: cExample. c */
  7. # Include "cExample. h"
  8. Int add (int x, int y)
  9. {
  10. Return x + y;
  11. }
  12. // C ++ implementation file, call add: cppFile. cpp
  13. Extern "C"
  14. {
  15. # Include "cExample. h"
  16. }
  17. Int main (int argc, char * argv [])
  18. {
  19. Add (2, 3 );
  20. Return 0;
  21. }

When C references functions and variables in C ++, the header file of C ++ needs to add extern "C ", however, you cannot directly reference this header file that declares extern "C" in C. You should only declare the extern "C" function defined in C ++ as the extern type.

 
 
  1. // C ++ header file cppExample. h
  2. # Ifndef CPP_EXAMPLE_H
  3. # Define CPP_EXAMPLE_H
  4. Extern "C" int add (int x, int y );
  5. # Endif
  6. // C ++ implementation file cppExample. cpp
  7. # Include "cppExample. h"
  8. Int add (int x, int y)
  9. {
  10. Return x + y;
  11. }
  12. /* C implementation file cFile. c
  13. /* Compilation errors: # include "cExample. h "*/
  14. Extern int add (int x, int y );
  15. Int main (int argc, char * argv [])
  16. {
  17. Add (2, 3 );
  18. Return 0;

If you thoroughly understand the role of extern "C" described in section 3rd in the compilation and connection phases, you can really understand the usage of referencing C Functions and C ++ functions from C ++ described in this section. Pay special attention to the sample code given in section 4th.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.