Precompiled directives #pragma there are many ways to use it, and I'm sure many programmers will never encounter some of them in their lifetime,
, they are really remote and we are not using their needs. So, this article will not be exhaustive, just put several commonly used several summary
A bit.
1. #pragma once
The guarantee header file is only once, equivalent to
#ifndef _x_h
#define _x_h
...
#endif//_x_h
2. #pragma comment
The prototype is #pragma comment ("Comment-type" [, commentstring]), put comment recode into the target file or can
Execute the file. "Comment-type" has five kinds, which Lib is more commonly used: #pragma comment (lib, "d3d9x") in the connection will be
The D3d9x.lib newspaper is included in the project. In fact, the IDE can also manually set the Lib file to include in.
3. #pragma warning
You can do some processing on the compiled warning.
For example, when you turn float to int, there is a warning. See warning is always unhappy, and some warning heart know, certainly not
What will cause the problem when:
#pragma warning (disable:4244)
The warning can be shielded. #pragma the scope of the warning is this file and, if it is a header file, it affects the module that contains it, ensuring that
Your use will not affect others. So it's best to restore warning in time.
#pragma warning (push)
#pragma warning (disable:4705)
#pragma warning (disable:4706)
Some Code
#pragma warning (POP)
can also
#pragma warning (disable:4705)
#pragma warning (disable:4706)
Some Code
#pragma warning (default:4705)
#pragma warning (default:4705)
4. #pragma pack
C + + class,struct,union is aligned by default in Word, double word, four word boundaries. Sometimes we need to align our way,
For example, let the network bag more brow.
#pragma pack (push,1)
struct a{//Now sizeof (A) equals 5; By default it is 8;
int i;
char c;
};
#pragma pack (POP) is the same as warning when used, also pay attention to scope problems. I remember a guy who used to change his head in a file.
Qi way, not in time to set back. This bug is very annoying, will intermittently and inexplicably cause the program crashes, must be careful.