Thread: static const VS #define

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    static const VS #define

    Is it a good style to always use static const rather than #define in C++ O.O. programming?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need "static" const either. But it's a good thing to use const wherever possible rather than define. Define is C-ish and not type safe. Use it for macros and avoid it for everything else, if you can.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I suppose it depends on what you're doing it for.

    If you just need a simple number remembered and used often, then a static const sounds better than #define.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    What's the advantage to use "static const" other than const, especially as a class member variable?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Static simply means there will only be a single instance of it. Well, simplified anyway. If you have a lot of classes, you can make it static, since you won't be needing more than one instance (the number won't change anyway!).
    But if it's not used in classes, and it's global, then there's no point. I'd rather make const variables that replaces defines global since they're just values to be read (and probably not read at all, but substituted in the code by the compiler in release mode).

    Quote Originally Posted by MacGyver View Post
    I suppose it depends on what you're doing it for.

    If you just need a simple number remembered and used often, then a static const sounds better than #define.
    I don't know if there's a disadvantage with const variables to replace define. Since it's const, you're telling the compiler the value won't ever change and it can optimize away the variable and put the real value it contains where it's used instead.
    Last edited by Elysia; 12-04-2007 at 12:19 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    If you are defining a constant to be shared among all the instances of the class, use static const. If the constant is specific to each instance, just use const (but note that all constructors of the class must initialise this const member variable in the initialisation list).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM