Definition, operator, and Declaration of the Protection characters and variables in the C ++ header file

Source: Internet
Author: User

Definition, operator, and Declaration of the Protection characters and variables in the C ++ header file

1. # ifndef # define # endif header file protection character

During the compilation process. the cpp file is regarded as a separate file to be compiled into a separate compilation unit. # ifndef ensures that the Class header file is in the same one. after being referenced multiple times in the cpp file, there will be no redefinition issues.

Note: It only prevents multiple references in the same. cpp file.

Example:

// file1.hclass file1{};// file2.h#include "file1.h"class file2{};// file3.h#include "file1.h"#include "file2.h"

The file3.h extension is as follows:

// The expanded content class file1 {}; // The expanded content class file1 {}; class file2 {};

At this time, the # ifndef header file protection is added to each file:


// file1.h#ifndef _FILE1_H_#define _FILE1_H_class file1{};#endif // !_FILE1_H_

// file2.h#ifndef _FILE2_H_#define _FILE2_H_#include "file1.h"class file2{};#endif // !_FILE2_H_

// file3.h#ifndef _FILE3_H_#define _FILE3_H_#include "file1.h"#include "file2.h"#endif // !_FILE3_H_

When expanding file3.h, _ FILE1_H _ will be defined only once, so there will be no redefinition error.




2. Declaration and definition of variables

Definition: used to allocate a bucket for a variable. You can also specify an initial value for the variable. In a program, variables have only one definition.

Declaration: used to indicate the type and name of the variable to the program. In a program, there can be multiple declarations. Definition is also Declaration: when defining a variable, we declare its type and name. You can use the extern keyword to declare a variable name without defining it: extern int I ;.

Example:

// file1.h#ifndef _FILE1_H_#define _FILE1_H_int a = 1;#endif // !_FILE1_H_

// file2.h#ifndef _FILE2_H_#define _FILE2_H_#include "file1.h"#endif // !_FILE2_H_

// main.cpp#include <iostream>int main(){extern int a;std::cout << a << std::endl;system("pause");return 0;}


At this time, an error will be reported during compilation, because a is defined in file1.h and a is defined in the expansion of file2.h.

Someone may ask, # Does ifndef prevent redefinition? # Ifndef is to prevent redefinition in the same file. In the above case, file1.h and file2.h are two different files, so int a = 1 appears at the same time in both file1.h and file2.h, A Redefinition error occurs.

So do not define variables in the header file. The best practice is to declare variables in the header file, define variables in. cpp, and declare the variables before using them.

Modify the example above:

// file1.h#ifndef _FILE1_H_#define _FILE1_H_extern int a;#endif // !_FILE1_H_

// file1.cpp#include "file1.h"int a = 1;

// file2.h#ifndef _FILE2_H_#define _FILE2_H_#include "file1.h"#endif // !_FILE2_H_

Main. cpp remains unchanged, so that it can run normally.

I want to declare the variables in the header file in the keil file and the C file definition header file, but when I declare the variables in the header file and then define them in the C file

Normally, the variable definition is put in the C file, not the header file, the C file definition, and the header file reference.
For example. c defines a variable unsigned char a; if you want. use this variable in the C file. You can. reference in C. the reference method is as follows: extern unsigned char;
You can also reference it in a header file. The reference method is the same: extern unsigned char;
After the header file is referenced, any C file that contains the header file can use the variable a, without additional reference.
One thing to note is that initialization is not allowed during reference. For example, variables defined in the X. c file are as follows:
Unsigned char a = 100; it can be initialized during definition. It cannot be initialized when the header file or other C files are referenced. Mom extern unsigned char a = 100; this is not acceptable, just reference it directly: extern unsigned char;

Function variables cannot be defined in the header file. Otherwise, repeated definition errors may occur when multiple C files contain the same header file.

The header file contains one sentence.

# Ifndef YOUR_HEADER
# Define YOUR_HEADER

# Endif

It can solve your problem.

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.