views:

156

answers:

3
// 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  A: 

MSDN says:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

Remember that including header files means to replace the "#include"-directive with the actual code of the header file. So, the static variables will be visible only in the ".cpp" (which is compiled) file that includes the two header files.

So each "cpp"-file including the headers will have it's own static variable.

Simon
Now I've declared/defined GasConstant in a Constants.h which is included into MyClass.h. Sounds like it will only be visible in files that include Constants.h or MyClass.h, and not from any other file that doesn't. What happens if another file includes both?
user144182
Each .cpp file will get its own, independent copy. May very well not be what you want...or maybe it is. For .cpp files that include both, this shouldn't change anything but you'll of course need the proper header protections.
Noah Roberts
So sounds like what I want to do is simply make it const.
user144182
+3  A: 

The use of static at namespace scope is deprecated in C++. It would normally only ever be seen in a source file, where it's 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 a nameless namespace.

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

James Hopkin
Recommended way to do this.... what exactly do you mean by "this"? I'm not sure its clear to me.
user144182
+1  A: 

If this is a header file, then static has no effect in this case. const objects already have internal linkage by default in C++, so whether you declare it with static or without static makes no difference whatsoever.

I assume you simply moved the declaration from the class into the namespace. But static has totally different meaning in the context of the class declaration and in the context of namespace. Inside the class, you needed static. In the namespace the static is superfluous.

AndreyT
@AndreyT - but isn't it the case that separate files that included the header would have separate instances of the namespace member as a result of static operating similar to file scope static? This won't matter of course since its const, i.e. this would be a problem if I thought the two files had access to the same member and updating from one would be reflected in the other. Just trying to keep it straight.
user144182
In fact, he doesn't need `static` in the class. With it and without it, both are invalid for `double` :)
Johannes Schaub - litb