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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:27+00:00 2026-05-16T19:44:27+00:00

I have a C++ code that’s a physics simulation tool. I would like to

  • 0

I have a C++ code that’s a physics simulation tool.

I would like to store some physical constants, conversion factor between different sets of units, and also some more application specific constants (such as definition like enum Planes {X=0, Y=1}) and I would like to be able to access them from everywhere in my code.

What would be the best way to do that ?

I think one way would be to create a namespace namespace constants (which can then be a nested namespace in my main namespace) with nested namespaces (like ‘constants’, ‘units’, etc.).

Would it be the way you would do that ?

If I use that method, do I have to make it a header file and include it everywhere ?

If I understand correctly the variables in the namespace at global scope have static linkage, but no external linkage. Then if I want to use them without including a file, I also have to declare them extern ?

As you can see I am a bit confused about that…

  • 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-16T19:44:27+00:00Added an answer on May 16, 2026 at 7:44 pm

    Namespace constants are the way to go in most cases.

    If I use that method, do I have to make it a header file and include it everywhere ?

    Yes, or not everywhere but only where it’s really USED.

    If I understand correctly the variables in the namespace at global scope have static linkage, but no external linkage. Then if I want to use them without including a file, I also have to declare them extern ?

    Yes, you have to do it that way :

    // header 
    
    namespace modulename
    {
      // maybe add another namespace to specify that you have constants, but taste-dependant
    
       namespace domain // like maths or physics
       {
            extern const Number THIS_NUMBER; // have to be defined in the cpp
            extern const int THAT_NUMBER = 256; // if it's int-based type, you can define it -here - BUT DON'T IF IT CAN BE CHANGED : all files including this one would have to be recompiled at each value change!!
       }
    }
    
    // .cpp, where you have the definitions
    
    namespace modulename
    {
       namespace domain // like maths or physics
       {
            const Number THIS_NUMBER = Number( 256.42f ); // definition - static is implicit
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that I would like to convert so that I am grabbing
I have code that will read in and process data that I would like
I have code that looks more or less like the code below but it
I have code that looks like this: obj.foo(); // obj might, or might not
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that's somewhat like this: ($i=0; $i < 100; $i++) { do
I have code that looks like this: public class A { public void doStuff()
I have code that basically looks like this (it isn't actually object, but rather

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.