Thread: is #define not recommended in C++ ?

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question is #define not recommended in C++ ?

    I find some #define in C codes but it seldom apprears in C++ codes in which book I am scanning. is that replaced by something already ?
    Never end on learning~

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    You can use a #define directive in C++, but it isn't recommended because you can't specify the data type of the constant...most people just use const.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    I got it. thanx guy~
    Never end on learning~

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is that replaced by something already ?
    In C #define is used for constant literals to make code more readable/portable, and for creating inline macros for efficiency. C++ recommends that const is used instead of #define for literals because more type checking is done and with the addition of the inline keyword for functions the #define macros are obsolete in C++.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    49
    The only usage of #define in C++: as preprocess instruct. like this:
    Code:
    #ifdef _DEBUG
        TRACE0("Some error occur...");
    #endif
    
        abort();
    Hello, everyone.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    thats not the only usage for #define in C++,
    if you are developing windows apps it is easier to use #define for your controls, ie:

    [EDIT]
    LoL, i was screwed up when i wrote this line:
    #define IDC_BUTTON1;
    it should be:
    #define IDC_BUTTON1 1000
    [/EDIT]

    Code:
    #define IDC_BUTTON1 1000
    CreateWindowEx(	0,
    		TEXT("BUTTON"), 
    		TEXT("Button 1"), 
    		WS_VISIBLE | 
    		WS_CHILD | 
    		BS_FLAT // My Favorite Button Style, 
    		0, 
    		0, 
    		100, 
    		30, 
    		hwnd, 
    		(HMENU)IDC_BUTTON1, 
    		hInstance, 
    		NULL);
    Last edited by Okiesmokie; 05-09-2002 at 06:07 PM.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Prelude, I know you are the man (or woman in this case ) when it comes to programming, but aren't #defines used a lot when it comes to window options, messages, and stuff like that?

  8. #8
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Thumbs up

    woh~

    hot discussion here, aha~

    and I am a bit confused with the code in ur posts. but one day I'll figure it out. thanx all guys who posted~
    Never end on learning~

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    49
    The usage of #define in Windows Programming is not in C++ Style. Because Windows SDK is based on C language. You can use const UINT instead of #define when you define an resource ID.

    the last three words should be: a resource ID. Excuse me. Oh, my poor English.
    Last edited by Hotman_x; 05-09-2002 at 10:00 PM.
    Hello, everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  3. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM