#define replacement?

This is a discussion on #define replacement? within the C++ Programming forums, part of the General Programming Boards category; I'm just wondering if there's now a more accepted way to define a set of values than #define. Say I ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    #define replacement?

    I'm just wondering if there's now a more accepted way to define a set of values than #define.

    Say I have the following:

    #define C_CMD1 1
    #define C_CMD2 2
    #define C_CMD3 3

    etc...

    Is there a more accepted way of doing this now that doesn't require #defines, and preferably is restricted in scope so the same name can be used in different classes with different values associated with them? I thought of CONST Variables but it seems like a waste of ram.
    Last edited by _Elixia_; 06-29-2003 at 05:39 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Using consts would be the only other way to do it, AFAIK.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I wouldn't say using const variables is a waste of RAM. Think about how much sapce an int takes (usually 4 bytes, sometimes 2 or 8) compared to the amount of memory you have access to (several megabytes).

    The type safety, and additional ease of debugging actual variables as opposed to #defines is well worth a few bytes per variable.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    And there's the fact that most modern compilers automatically substitute const non-user-defined variables with literals. So there's no wasted memory.

    I know this doesn't cover user-defined classes; but then again, neither does #define.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    The prefered way to define constants in C++ is using the keyword const.
    Mr. C: Author and Instructor

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thank you, Mr Obvious.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 05:28 PM
  3. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 06:11 AM
  4. edit controls in dialogs
    By Homunculus in forum Windows Programming
    Replies: 10
    Last Post: 02-23-2006, 03:38 PM
  5. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21