Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1111719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:35:34+00:00 2026-05-17T02:35:34+00:00

Possible Duplicate: static const vs #define In C++, to define a constant to be

  • 0

Possible Duplicate:
static const vs #define

In C++, to define a constant to be used across the entire application, what is the usual practice?

#define WINDOWS_HEIGHT 1024

or

const int WINDOWS_HEIGHT = 1024;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:35:34+00:00Added an answer on May 17, 2026 at 2:35 am

    Pros and cons to everything, depending on usage:

    • enums
      • only possible for integer values
      • properly scoped / identifier clash issues handled nicely
      • strongly typed, but to a big-enough signed-or-unsigned int size over which you have no control (in C++03)
      • can’t take the address
      • stronger usage restraints (e.g. incrementing – template <typename T> void f(T t) { cout << ++t; } won’t compile)
      • each constant’s type taken from the enclosing enum, so template <typename T> void f(T) get a distinct instantiation when passed the same numeric value from different enums, all of which are distinct from any actual f(int) instantiation.
      • even with typeof, can’t expect numeric_limits to provide useful insight
      • the enum’s typename may appear in various places in RTTI, compiler messages etc. – possibly useful, possibly obfuscation
    • consts
      • properly scoped / identifier clash issues handled nicely
      • strong, single, user-specified type
      • One Definition Rule complications
    • defines
      • "global" scope / more prone to conflicting usages, which can produce hard-to-resolve compilation issues and unexpected run-time results rather than sane error messages; mitigating this requires:
        • long, obscure and/or centrally coordinated identifiers, and access to them can’t benefit from implicitly matching used/current/Koenig-looked-up namespace, namespace aliases etc.
        • use of all uppercase characters is generally required and reserved for preprocessor defines (an important guideline for enterprise scale preprocessor usage to remain manageable, and which 3rd party libraries can be expected to follow), observation of which implies migration of existing consts or enums to defines involves a change in capitalisation (and hence affects client code). (Personally, I capitalise the first letter of enums but not consts, so I’d be hit here anyway – maybe time to rethink that.)
      • more compile-time operations possible: string literal concatenation, stringification (taking size thereof)
        • downside is that given #define X "x" and some client usage ala "pre" X "post", you’re in trouble if you want or need to make X a runtime-changeable variable rather than a constant, whereas that transition is easier from a const char* or const std::string given they already force the user to incorporate concatenation operations.
      • can’t use sizeof directly on a defined numeric constant
      • untyped (GCC doesn’t warn if compared to unsigned)
      • some compiler/linker/debugger chains may not present the identifier, so you’ll be reduced to looking at "magic numbers" (strings, whatever…)
      • can’t take the address
      • the substituted value need not be legal (or discrete) in the context where the #define is created, as it’s evaluated at each point of use, so you can reference not-yet-declared objects, depend on "implementation" that needn’t be pre-included, create "constants" such as { 1, 2 } that can be used to initialise arrays, or #define MICROSECONDS *1E-6 etc. (definitely not recommending this!)
      • some special things like __FILE__ and __LINE__ can be incorporated into the macro substitution

    As a general rule, I use consts and consider them the most professional option for general usage (though the others have a simplicity appealing to this old lazy programmer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: static const vs #define in C I started to learn C and
Possible Duplicate: “static const” vs “#define” in c I am going through the K&R
Possible Duplicate: "static const" vs "#define" in c A macro is a fragment of
Possible Duplicate: C++ static constant string (class member) static const C++ class member initialized
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate Why can't I have a non-integral static const member in a class?
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
Possible Duplicate: ASP.NET Application state vs a Static object I am using the static
Possible Duplicate: Static Block in Java I came across the following use of static
Possible Duplicate: C#: Static readonly vs const Which is preferable in this instance (no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.