   Is this legal?
class SomeClass{    ...};namespace Test{static SomeClass Some;}It compiles but when I try to run my program it quits with an memory error.  Not sure if it's related to this, or something else.   
  1.)  What purpose do you think the static keyword serves here anyway?  Your variable will always be accessible to any file that includes your namespace definition via Test::Some, and it's not like you're going to have multiple instances of your namespace.2.)  Post the entire source code of your program, if you think the error might be coming from somewhere else.   ;  Doesn't static here mean that each cpp file including the header gets its own instance of Test::Some?   ;  Where is this code located, in a header or in a .cpp file? I think static is doing things you probably don't expect - but that's a side issue.Your memory error is almost 100% guaranteed not coming from that code [wink]   ;  It's legal, the static is probably wrong and you haven't shown us enough code for us to really help.   ;  Erm, is this in C++ or C# or something else? :s   ;Quote:Original post by visitorDoesn't static here mean that each cpp file including the header gets its own instance of Test::Some?Your phrase is slightly confusing (I first read it "all cpp files plus the header", but I guess you mean "all cpp files that include the header"), but yes, you are right:All compilation units (source code files) that #include the definition of a static instance get their own instance of it. If you want all compilation units to really share a single instance, declare it "extern" in a common header, and define it in exactly one compilation unit.Basically, when you #include that definition, an amalgamized merger of your sourcecode file and all files it #includes is created by the preprocessor:file-astatic X x;file-b#include "file-a"int main () {}file-c#include "file-a"int booya () {}after preprocessing:tmp-file-astatic X x;int main () {}tmp-file-bstatic X x;int booya () {}The "static" in this context is for internal linkage, meaning that tmp-file-a and tmp-file-b both have their own, private instance x of X (each of their instances x is also invisible to each other file). The same can be achieved by using anonymous namespaces in C++. Also, putting a static declaration inside a namespace doesn't change that behaviour, because the only purpose of namespaces is to decorate names (which has tons of consequences under the hood). Further, putting a linkage-static qualifier to a variable inside an anonymous namespace is redundant.   ;Quote:Original post by ZahlmanErm, is this in C++ or C# or something else? :sIma guess C++ since there is a semicolon at the end of the class definition.Anyway, scarypajamas, it is totally legal to declare a static instance of a class.  So the problem must lie in some other part of the program that we can't see from your code (maybe the class definition, maybe where those blocks of code are actually used, etc.).  Can you give us more info?   ;Quote:Original post by MikeTacularQuote:Original post by ZahlmanErm, is this in C++ or C# or something else? :sIma guess C++ since there is a semicolon at the end of the class definition.It is also valid C#, except for the ellipsis, which is also invalid in C++ :P   ;Quote:Original post by phresnelQuote:Original post by MikeTacularQuote:Original post by ZahlmanErm, is this in C++ or C# or something else? :sIma guess C++ since there is a semicolon at the end of the class definition.It is also valid C#, except for the ellipsis, which is also invalid in C++ :PI never said it wasn't C#...  Then again maybe it's some super language he's created where the compiler automatically fills the "..." with all the code you want and makes the entire program for you [grin]   