C++ 2010에서 헤더가 중복 Include 되는 것을 막기위해서 사용하던 #ifndef/define/endif 3개의 조합대신에
간단하게 사용할 수 있는 #pragma once 를 지원합니다.
C++Builder 2007 HELP에는 안나오는것으로 봐서
2009나 2010부터 지원하는듯 합니다.
Visual C++에서는 전부터 지원했던것 같습니다.
[기존 방법]
#ifndef XXXXXXX
#define XXXXXXX
#endif
[간단한 방법]
#pragma once
즉 헤더파일 제일 위에 간단히 #pragma once 만 추가하면됩니다.
C++도움말 긁어온것
Syntax
#pragma once
Description
#pragma once is a preprocessor directive used to prevent header files from being included multiple times. The #pragma once directive, once present in a file, assures that the file will not be included multiple times in the current project.
An example of why #pragma once is useful is the fact that a file that includes another header is sufficient. For a logical example where the directive can be used, see the code below.
Vertex.h
#pragma once
struct Vertex{
int x_coord;
int y_coord;
int z_coord;
};
Triangle.h
#include "Vertex.h"
struct Triangle{
Vertex p1;
Vertex p2;
Vertex p3;
};
Square.h
struct Square{
Vertex v1;
Vertex v2;
Vertex v3;
Vertex v4;
};
Drawing.cpp
#include
#include "Triangle.h"
#include "Vertex.h"
int main()
{
//a triangle
Triangle tr0;
//a square
Square sq0;
return 0;
}
Because both the triangle and the square need the vertex struct to know their position in three-dimensional space, both of them include Vertex.h. The triangle and the square are included too in the main .cpp, therefore the Vertex.h file would be included twice (by Triangle.h and by Square.h). Using #pragma once in Vertex.h prevents the preprocessor from including the file a second time. As a result, no error is thrown.
Note: The same thing can be achieved with #ifndef/#define/#endif, which is the ANSI standard way of dealing with the double inclusion problem.
#if _MSC_VER > 1000
#pragma once
#endif
이렇게 구현되어 있는걸로 봐서는 빌더 4.0부터 지원되는거 같습니다.