Thread: const vs. #define

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    79

    const vs. #define

    One C++ programming book I have said that it's a better idea to use

    const type x = value;
    than
    #define x value

    Is there any truth to this? Which do you use?

    I can think of two conceivable reasons to use const:
    Constants have a type, which is important in a strongly typed language like C++.

    Const may only need to be written into the program once. Not quite sure about this one, but it's possible that when using const, the compiler can use the same copy of the constant multiple places within the program.

    Anybody have any useful information on this?
    All generalizations are false

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I use const all the time..... in fact I only really use #define for inclusion guards or conditional compilation. I prefer inline functions to macros. I know some functions wont be inlined that would be had i used a macro but i guess that if the function doesn't get inlined then it shouldn't be inlined anyway.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    They are nearly the same thing. The #define is global, the other is not.
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I use enums for integer type values, because I can keep it local to the
    class.

    You sometimes get the problem

    Code:
    class C {
    public:
    
    private:
             // want a constant local to the class 
             static const int num = 4;         
             enum {
                       NUM = 4
              };
    };
    Outside of classes, most c++ people will tell you to use const.
    Thinking in c++ gives a very pretty good explanation of this.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    though they say one is better... i belive this is untrue since both have valid purposes use what fits the situation.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    13

    Summary...

    This is my personal preference for using constants:

    Const:
    Numerical constants and similar
    Why?
    Strong typechecking.
    Can limit scope of const. ( to eg. a function )

    Enum:
    Logical grouping of numerical constants
    enum fruit { apple, orange, pear }
    'Selection' parameter to function.
    myFunc( fruit selectFruit );
    Why?
    Again typechecking, (compare to myFunc( int selectFruit) )

    #define:
    Preprocessor related paramer settings
    With #include to avoiding double codeinclution.
    Why?
    There is no other nice way to do it, (to my knowlege).

    If you can think up any objections I would be glad to hear them..

    - Anders

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. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  3. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  4. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM