Basic concepts of the C ++ include Mechanism

Source: Internet
Author: User
Tags define definition

In a powerful language such as C ++ programming language, there is a lot of profound content that deserves further research. For example, the C ++ include mechanism introduced today is one of the hard-to-understand aspects. Let's take a look at this.

The # include commands for c/c ++ programming are not unfamiliar, and the vast majority of them know how to use them. But I believe there are still some people who know this.

 
 
  1. C:  
  2. #include < stdio.h> 
  3. C++:  
  4. #include < iostream> 

Contains the C/C ++ standard input header file. The include command is not limited to. h header files, but can contain any C/csf-workflow code file that the compiler can recognize, such as. C,. hpp,. cpp,. hxx,. cxx, and. txt and. abc.

  • Overview of C ++ Cstring Application Methods
  • Analysis of C ++ typeof Basic Application Methods
  • Introduction to the C ++ access control operator
  • Detailed Usage Guide for C ++ member function pointers
  • Overview of the basic content of the C ++ explicit keyword

1. C ++ include mechanism Glossary

Preprocessing preprocess)

A mechanism set up to facilitate compiler processing, including some commonly used preprocessing commands and statements, which are collectively referred to as preprocessing systems.

For example, # include # define # if... # else... # endif # pragma. The implementation of these commands is determined by the compiler (implementation specified) to mention the pre-processing commands. By the way, the header files prevent repeated inclusion of the two methods.

A. Protect macros

 
 
  1. # Ifndef _ ABCDE_H
  2. # Define _ ABCDE_H
  3. /*
  4. Code Section
  5. */
  6. # Endif

After being included once, the macro _ ABCDE_H already exists, and the code from # define _ ABCDE_H to # endif will be skipped next time.
There is also an instruction supported by a specific compiler:

 
 
  1. b.#pragma once 

Ensure that the file is physically compiled only once, and prevent repeated inclusion.

However, the two methods are different:

A. macro guard is highly portable and is supported by most compilers. If you accidentally copy a few identical codes, the Macro name will not conflict with other macros, otherwise, you may be confused when the compiler reports a lot of errors;

B. # The pragma once Command is simple. It ensures that the file is physically compiled only once. You do not have to think about different macro names. However, if there are several copies of the file, obviously, it does not work.

Declare declaration)

It refers to introducing a name to the current compilation unit, or re-declaring a previously declared name, which specifies how to interpret a name and its attributes;
For example:

 
 
  1. Int main (void)
  2. {
  3. Int a; // declares the variable a, whose type is int.
  4. Int * pa; // declares the variable pa, which is a pointer type pointing to the int type.
  5. }

Define definition)

Except for the following situations, the Declaration is the definition

A. Declare a function, but it does not include a function body;

B. The Declaration contains the extern link qualifier, for example, extern int;

C. The Declaration neither has the initialization syntax nor the function body;

D. declare static data members in the class declaration;

E. Class Name Declaration;

F. typedef Declaration;

G. using declaration or using command;

The above situation applies to the features of C, and C ++ is fully applicable. In general, it is defined to allocate or reserve storage space for its objects, and declarations are not used.

Compilation unit translation unit)

A source file ,. c. cpp, etc. Together with the files it contains, a source code file is formed after preprocessing. The standard called translation unit (compilation unit) includes a series of statements and definitions; a program (program) it consists of one or more compilation units. The compiler compiles each translation unit into the target code (. obj), connect these compiled compilation units (the target code) through the connector (linker) to a complete command sequence executable file, static library, dynamic library, and so on ).

One-time rule definition (rule)

It means that only one compilation unit can be entered into the connection.

2. C ++ include mechanism views and Examples

A: Only declare the header file.

 
 
  1. example_a.h  
  2. void function();  
  3. example_a.cpp:  
  4. #include "example_a.h"  
  5. void function()  
  6. {} 

B. Use of standard header files

All the content in the latest C ++ standard library is stored in the namespace std and the namespace content is invisible to the outside world), but it brings a new question, countless existing C ++ Code relies on features in the pseudo-standard library that have been used for many years, such as declarations in <iostream. h> Using std to package the standard library causes unavailability of existing code. To be compatible with this situation, the Standards Board has created a new header file for the part of the standard library that wraps std. The new header file name is the same as the old one, but it does not. h, such as <iostream. h> changes to <iostream>. The C header file adopts the same method, but the character c is added before each header file name, such as <string. h> is changed to <cstring>, <stdio. h> changed to <cstdio>. It is best to use the new file header. To use the C ++ program of the new file header, you need to use the class name specified by using namespace std or using namespace std, to make the required classes visible to our code.

3. Summary

Since it is often used, we should understand its principles, reduce the confusion during programming, and improve programming efficiency. The above is an introduction to the C ++ include mechanism.

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.