-
May 18th, 2009, 02:23 AM
#1
what does #define do when there is no replacement
I know that define is a search and replace thing but I dont get what define is doing when the code is like this
Code:
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <string>
using namespace std;
class date
{
private:
enum m_e_month //member enumeration named month
{
January=1,
Febuary=2,
March=3,
April=4,
May=5,
June=6,
July=7,
August=8,
September=9,
October=10,
November=11,
December=12
};
m_e_month m_m_e_month_month; //its a member variable m_ of type (m_e_month) named month
unsigned short int m_sh_I_year;
unsigned short int m_sh_I_day;
void makeyear()
{
cout<<"enter the year:";
int a;
cin>>a;
}
blah{}int float month m_m_e_month_mohnth {} ; makgeblah((boool asdfhpsjdj609/^#g) hfljgfc...
};
#endif
what exactly is "#define DATE_H" defining it certainly isn't making any macros or replacing anything so what does it do and what is it's purpose when there is no replacement?
(When I mean replacement I mean the "1234" in
#define abcd 1234
)
-
May 18th, 2009, 02:51 AM
#2
Re: what does #define do when there is no replacement
If you put DATE_H in your code somewhere the compiler would replace it with no code.
This is actually used sometimes for compiler-based situations in portable code where you need to insert certain key words for one compiler/system and not another. Also language-based (for example you want to use extern "C" if compiling for C++ but not if compiling for C).
A typical situation might be importing or exporting a DLL function: in Windows you need to put __declspec( dllimport ) or __declspec( dllexport ) but for a UNIX Shared-Object you do not use any key word at all. So your declaration might be:
Code:
#ifdef WIN32
#ifdef BUILDING_FOO
#define EXPORT_FOO __declspec( dllexport )
#else
#define EXPORT_FOO __declspec( dllimport )
#endif // BUILDING FOO
#else
#define EXPORT_FOO
#endif
int EXPORT_FOO foo_function( void * param );
-
May 18th, 2009, 03:42 AM
#3
Re: what does #define do when there is no replacement
Originally Posted by E-man96
what exactly is "#define DATE_H" defining it certainly isn't making any macros or replacing anything so what does it do and what is it's purpose when there is no replacement?
NMTop40 discussed this more generally, but in this case you are looking at a header inclusion guard. The idea is to ensure that even when the same header is included more than once in the same translation unit (as in combination of source file and the headers that it includes, directly or indirectly), only one copy of the header is actually included.
-
May 18th, 2009, 04:03 AM
#4
Re: what does #define do when there is no replacement
#define <xxx> [<yyy>]
actually adds xxx to the table of defined symbols.
In the special case
yyy is empty which means: xxx is defined as nothing.
There is a difference between "is not defined" and "is defined as nothing ". Therefore you can check for xxx whether it has been defined alredy or not.
In your example it is used for including the header file only once as laserlight alredy stated.
In general you can use this feature to check for any conditions defined by a certain #define directive. There is a #undef too (used rarely).
With regards
Programartist
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|