CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    24

    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
    )

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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 );

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what does #define do when there is no replacement

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    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
    Code:
    #define DATE_H
    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
  •  





Click Here to Expand Forum to Full Width

Featured