pragma once
-
Hi All, When we include a class, the wizard creates two files (.h and .cpp) in the class's name. The .cpp file starts with like this:
#ifndef _FILE1_H
#define _FILE1_H
#include "File1.h"
#endifHope this is a way to avoid including the file (.h) more than once in the application. How this .cpp file is getting related with its corresponding .h file? Because, we include only .h file. Kindly let me know that how can compare this with "# pragma once" and more about "pragma". Thanks in advance, Sarvan AL
*** Live Life To Its Fullest ***
-
Hi All, When we include a class, the wizard creates two files (.h and .cpp) in the class's name. The .cpp file starts with like this:
#ifndef _FILE1_H
#define _FILE1_H
#include "File1.h"
#endifHope this is a way to avoid including the file (.h) more than once in the application. How this .cpp file is getting related with its corresponding .h file? Because, we include only .h file. Kindly let me know that how can compare this with "# pragma once" and more about "pragma". Thanks in advance, Sarvan AL
*** Live Life To Its Fullest ***
Sarvan AL wrote:
Hope this is a way to avoid including the file (.h) more than once in the application
Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:
#ifndef _FILE1_H
#define _FILE1_H.. here the declarations
#endif
In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
-
Sarvan AL wrote:
Hope this is a way to avoid including the file (.h) more than once in the application
Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:
#ifndef _FILE1_H
#define _FILE1_H.. here the declarations
#endif
In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
-
Sarvan AL wrote:
Hope this is a way to avoid including the file (.h) more than once in the application
Yes, it is. However, the #ifdef/#define##endif should normally be located inside File1.h, and not where it is called. In File1.h:
#ifndef _FILE1_H
#define _FILE1_H.. here the declarations
#endif
In the .cpp #include "File1.h" Otherwise, you need to reproduce the ifndef/endif block for each include of the File1.h, which is no good practice (unless you have some hardware limitation, like the h file is located on some network that is lengthy to be accessed and that the simple fact of going inside the include lasts 5 seconds or more). #pragma are directive to the precompiler, e.g. the software that runs one time through the code before the compiler. It replaces all occurences of #defines with their real value, copies the content of includes inside the cpp file, etc.. The precompiler can be given some directives using #pragma, such as #pragma disable(warning:1020) which disables warning 1020. Meaning and sometimes existence of pragmas depends on the compiler. #pragma once is a directive that (for visual c++, but also other compiler) says that the file in which it is located must only be included once. It is equivalent to the code block I've provided you with.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Adding to Rage explanation on #pragma once, the header file is scanned only once(internally it is maintained in a table)where as
Rage wrote:
#ifndef _FILE1_H #define _FILE1_H .. here the declarations #endif
if the header file is included more than once in a .cpp file,it is scanned that no times -- modified at 8:04 Wednesday 16th August, 2006 -- modified at 8:05 Wednesday 16th August, 2006
never say die