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 606495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:15:37+00:00 2026-05-13T17:15:37+00:00

I want to define a constant in C++ to be visible in several source

  • 0

I want to define a constant in C++ to be visible in several source files.
I can imagine the following ways to define it in a header file:

  1. #define GLOBAL_CONST_VAR 0xFF
  2. int GLOBAL_CONST_VAR = 0xFF;
  3. Some function returing the value (e.g. int get_GLOBAL_CONST_VAR())
  4. enum { GLOBAL_CONST_VAR = 0xFF; }
  5. const int GLOBAL_CONST_VAR = 0xFF;
  6. extern const int GLOBAL_CONST_VAR;
    and in one source file const int GLOBAL_CONST_VAR = 0xFF;

Option (1) – is definitely not the option you would like to use

Option (2) – defining instance of the variable in each object file using the header file

Option (3) – IMO is over killing in most cases

Option (4) – in many cases maybe not good since enum has no concrete type (C++0X will add possibility to define the type)

So in most cases I need to choose between (5) and (6).
My questions:

  1. What do you prefer (5) or (6)?
  2. Why (5) is ok, while (2) is not?
  • 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-13T17:15:38+00:00Added an answer on May 13, 2026 at 5:15 pm

    (5) says exactly what you want to say. Plus it lets the compiler optimize it away most of the time. (6) on the other hand won’t let the compiler ever optimize it away because the compiler doesn’t know if you’ll change it eventually or not.

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

Sidebar

Related Questions

I want to define a constant inside a function that could be used outside
Possible Duplicate: C#: How to enumerate an enum? Hi all I want to know
So I want to find the best method of allowing my Logger class to
I am using a constant NEWS_POST_NUMBER and i am getting confused on how to
In a new rail 3 app I want to replace prototype with jquery, but
EDIT: My example might have created some confusion. I have changed the example below
I have a class with a data attribute, say of type int , which
in a C program I have this variable: float (*data)[3]; data = (float (*)[3])calloc(ntimes,
which is the cleanest way to use something like a global variable? Normally, using
Initially i thought to do something like: #EXIT CODES class ExitCode(object): (USERHOME_INVALID, \ USERHOME_CANNOT_WRITE,

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.