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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:05:16+00:00 2026-06-10T01:05:16+00:00

A program I am working on has many constants that apply throughout all classes.

  • 0

A program I am working on has many constants that apply throughout all classes. I want to make one header file “Constants.h”, and be able to declare all the relevant constants. Then in my other classes, I can just include #include "Constants.h.

I got it to work fine using #ifndef … #define ... syntax. However, I would prefer to use the const int... form of constants. I’m not quite sure how to though.

  • 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-06-10T01:05:17+00:00Added an answer on June 10, 2026 at 1:05 am

    You could simply define a series of const ints in a header file:

    // Constants.h
    #if !defined(MYLIB_CONSTANTS_H)
    #define MYLIB_CONSTANTS_H 1
    
    const int a = 100;
    const int b = 0x7f;
    
    #endif
    

    This works because in C++ a name at namespace scope (including the global namespace) that is explicitly declared const and not explicitly declared extern has internal linkage, so these variables would not cause duplicate symbols when you link together translation units. Alternatively you could explicitly declare the constants as static.

    static const int a = 100;
    static const int b = 0x7f;
    

    This is more compatible with C and more readable for people that may not be familiar with C++ linkage rules.

    If all the constants are ints then another method you could use is to declare the identifiers as enums.

    enum mylib_constants {
        a = 100;
        b = 0x7f;
    };
    

    All of these methods use only a header and allow the declared names to be used as compile time constants. Using extern const int and a separate implementation file prevents the names from being used as compile time constants.


    Note that the rule that makes certain constants implicitly internal linkage does apply to pointers, exactly like constants of other types. The tricky thing though is that marking a pointer as const requires syntax a little different that most people use to make variables of other types const. You need to do:

    int * const ptr;
    

    to make a constant pointer, so that the rule will apply to it.

    Also note that this is one reason I prefer to consistently put const after the type: int const instead of const int. I also put the * next to the variable: i.e. int *ptr; instead of int* ptr; (compare also this discussion).

    I like to do these sorts of things because they reflect the general case of how C++ really works. The alternatives (const int, int* p) are just special cased to make some simple things more readable. The problem is that when you step out of those simple cases, the special cased alternatives become actively misleading.

    So although the earlier examples show the common usage of const, I would actually recommend people write them like this:

    int const a = 100;
    int const b = 0x7f;
    

    and

    static int const a = 100;
    static int const b = 0x7f;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a program that has many of those the user SHOULD
I need to restart the program that im working on after an update has
I'm working on a program that uses HTML/CSS/Javascript/JQuery for its user interface. One of
I'm working on a program that processes many requests, none of them reaching more
I'm working on a program that needs a feature that has similar functionality as
I am working on a program that take user input for two file names.
how can i make java program working with multiple languages (frensh, english , arabic
I have a working program in C++ that generates data for a Mandelbrot Set.
I'm working on an older project, updating it. Part of the program has a
I am currently working on a FORTRAN program that is to read an input

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.