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

  • Home
  • SEARCH
  • 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 808353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:32:55+00:00 2026-05-15T00:32:55+00:00

// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; class MyClass {

  • 0
// MyClass.h

namespace MyNamespace {

  static const double GasConstant = 1.987;

  class MyClass
  {
    // constructors, methods, etc.
  };
}

I previously had GasConstant declared within the MyClass declaration (and had a separate definition in the source file since C++ does not support const initialization of non-integral types). I however need to access it from other files and also logically it seems like it should reside at the namespace level.

My questions is, what effect does static const have in this case? Clearly const means I can’t assign a new value to GasConstant, but what does a static member at the namespace mean. Is this similar to static at file scope, where the member is not accessible outside of the unit?

  • 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-15T00:32:56+00:00Added an answer on May 15, 2026 at 12:32 am

    The use of static at namespace scope is was* deprecated in C++. It would normally only ever be seen in a source file, where its effect is to make the variable local to that source file. That is, another source file can have a variable of exactly the same name with no conflict.

    In C++, the recommended way to make variables local to the source file is to use an anonymous namespace.

    I think it’s fair to say the static in the header in your code is simply incorrect.

    *As pointed out by Tom in the comments (and in this answer), the C++ committee reversed the decision to deprecate static use at file scope, on the basis that this usage will always be part of the language (e.g. for C compatibility).

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

Sidebar

Related Questions

public class MyClass { public int Age; public int ID; } public void MyMethod()
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively
Suppose I have a class public class MyClass { private Set<String> set = new
Given this definition: [UniqueId(Ident)] public class MyClass : MyBase { public int Ident{ get;
If I have a class org.foobar.MyClass and want to put it in a JAR
Is it possible to do something like this: namespace MyNamespace { <-- blank line
I can forward declare a function in a namespace by doing this: void myNamespace::doThing();

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.