How do you const?

This is a discussion on How do you const? within the C++ Programming forums, part of the General Programming Boards category; Less of a question, and more of a survey really. How do you define constants in your code? #define, enum ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    How do you const?

    Less of a question, and more of a survey really. How do you define constants in your code? #define, enum or just const? What benefits are there (if any) to using any particular method?

    fishybawb

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    A #define is evaluated at compilation time, while a const is evaluated at run-time. Plus a lot of places find the use of #define's in C++ code bad so you should use const, but there are a few situations where #defines are helpfull, such as conditional compilation, the creation of header files, and so on.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Unregistered
    Guest
    Mostly consts with the occasional enum if they are related, eg

    Code:
    enum DIRECTION
    {
         LEFT = -1,
         NONE = 0,
         RIGHT = 1
    };

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I like const because it allows greater flexibility, const can be applied to member functions, class objects, etc, and also you can use cheats like const_cast and mutable to modify const, whereas #define leaves you stuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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