VC++ -- Difficult question !!

Status
Not open for further replies.

ahmed osama

Full Member level 6
Joined
Jul 18, 2004
Messages
352
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,296
Location
Cairo, Egypt, Egypt
Activity points
2,652
If any one use VC++ for creating a new class ,will find some strange things written in the start of the file

// CEquParser.h: interface for the CEquParser class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CEQUPARSER_H__ACCB3DAE_4F18_4E1B_B084_CEF3AB97079A__INCLUDED_)
#define AFX_CEQUPARSER_H__ACCB3DAE_4F18_4E1B_B084_CEF3AB97079A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CEquParser
{
public:
CEquParser();
virtual ~CEquParser();

};

#endif // !defined(AFX_CEQUPARSER_H__ACCB3DAE_4F18_4E1B_B084_CEF3AB97079A__INCLUDED_)


any one has idea what is that?? and Why
 

SHAME ON YOU if you don't know the first #ifndef (or #if !defined(...) ) / #define statements ;-). But you want to learn what they mean so that's a GOOD thing!

They are a must in header files!

It is to guard the C files for multiple inclusions of header files.

Example:

---types.h---
typedef unsigned char uint8
------------------

---serialPort.h---
#include "types.h"
-----------------

---sockets.h---
#include "types.h"
------------------

---socketToSerial.cpp---
#include "serialPort.h"
#include "sockets.h"
// Here you will get the error that the type uint8 is defined multiple times
---------------------------

And when you use the guarding statements, the second inclusion of types.h will not be done in the C file. The first time it reaches the #ifndef it sees that this Preprocessor variable hasn't been defined so it goes on and evaluates the code between the first #ifndef and #endif at the bottom of the file (and defines the Preprocessor variable. The second time it wants to include types.h (from sockets.h) it will see that the Preprocessor variable has been defined so it will skip the second time types.h is included somewhere. (So everything is included once and you don't have to pay attention to that matter!

The name of the variable is free. I use logical names most of the time (like the name of the class with _h_ behind it.) Microsoft Visual C++ uses unique identifiers without any logical meanig (it just is unique ;-) ). You're free to choose whatever you like (as long as each header file in your project uses a different name)



The second
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

seems to be the microsoft version of the guarding
from MSDN:
#pragma once
Specifies that the file, in which the pragma resides, will be included (opened) only once by the compiler in a build. A common use for this pragma is the following:

I'm sure it has it's good things too. It seems to be redundant. (although I think it can speed up your build process in large projects.)
 

I know #ifndef (or #if !defined(...) ) / #define statements . but i thought maybe any one know such unique thing what it means..how the compiler created??
 

I'm sorry about the 'shame on you'. It's just that i have know a lot of "programmers" (<- Notice the quotes) who just said: "I don't know what this does, so i remove it!". So it was not directect at your person, please don't be offended by it!

The short explanation from the above:

This sequence:
#ifndef TheUniqueVariableGuardingTheMultipleInclusionOfThisHeaderFile_h_
#define TheUniqueVariableGuardingTheMultipleInclusionOfThisHeaderFile_h_
...
#endif // TheUniqueVariableGuardingTheMultipleInclusionOfThisHeaderFile_h_

is used to guard a header file from being included multiple times in a C/C++ file before compiling.
As you probably know a typedef can only be defined once while compiling a C/C++ file. When a header file (which contains a typedef) is included twice in a C/C++ file the compiler will generate an error that the typedef was defined before.
Using the guarding statements, this will not happen!

It's the reason for using them! You should use them because it will make coding a big project allot easier!



To explain what it does exactly i will use the example from my previous post. Before the compiler compiles socketToSerial.cpp, the preprocessor is run. This will generate a new socketToSerial.cpp with all preprocessor commands processed.
The new file will look like this:
---socketToSerial.cpp---
typedef unsigned char uint8 // from the #include "serialPort.h"
typedef unsigned char uint8 // from the #include "sockets.h"
---------------------------
And when the compiler tries to compile this file, it will generate an error!

When the guarding statements where used here, the folowing file would have been generated:
---socketToSerial.cpp---
typedef unsigned char uint8 // from the #include "serialPort.h"
// from the #include "sockets.h" (the guarding variable for types.h was already defined so nothing here!
---------------------------

Antharax
 

A ittle anotation...

.... maybe any one know such unique thing what it means..how the compiler created??


The compiler dosent create that file, was the IDE with a wizard, how is generated, well I will have a function that get a rndom number, then convert it to a decimal ASCII and concatenate it with the name of the file. Dont know there are a lot of way to obtain a number and only need by probable that will not be the same the next time, for proof make a new project with the same arguments to time at diferent locations, if the umbers concatenated at the end are the same, the function for obtain such numbers is more like a hash or some technique based on the name, if they are not equal the function that return such calues is more likely a random function.... the point is that the wizard of the IDE try to guarantee that you will not have in two diferent files the same guard var. You can modify such things with your normall #ifndef xxx_h_ that you use and the thing should comile equal....




compiler, linker and static libraries and import libraries (for import names in dinamyc link libraryes), and the IDE are diferent things.


By the way If Im no wrong the versions that are generated automatically are something about the include files and the versión of Windows and the so called Plataform SDK such definition serve for know if you are running under Windows 2003 Server, XP, or other and what include files you have....


They are for Windows versioning .
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…