Fog Creek Software
Discussion Board




static class members

boring question...

Why do you have to write

class x {
  static int val;
};

int x::val = 42;


When you could just go

class x {
  static int val = 42;
};

After all, it's just a glorified global variable.

Alex.ro
Wednesday, January 07, 2004

I just tested this:

public class Class2
{
    public static int x = 42;
    public Class2()
    {
    }
}

And it compiled. Is that what you mean?

Chris Ormerod
Wednesday, January 07, 2004

And I tested utilising that static variable like this:

class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
    Console.WriteLine(Class2.x.ToString());
    }
}

Yes, I just used a default console project, but it shows that what you want to do works.

Chris Ormerod
Wednesday, January 07, 2004

Judging by the trailing semi-colon for the first post, and lack of such in the second post, the first post is talking about C++, and the second about C#.

If you're talking about C++, from memory the standard says you can write it the short way, although older compilers (like MSVC++ 6) don't support it.

Sum Dum Gai
Wednesday, January 07, 2004

iirc, only int static members can be initialised in the declaration, rather than needing a definition.  I think codewarrior allows floats to be initialised in place as well.  At any rate, things are limited to primatives.

Complex types have to be initialised as if they were extern.  They are infact normal global variables with a scoped name.

One subtle issue with static data is initialisation order.  That seems compiler dependant.

i like i
Wednesday, January 07, 2004

Well there you go, I don't do C++ - sorry.

Chris Ormerod
Wednesday, January 07, 2004

What did you think the text "x::val" would represent in C#?

grouch
Wednesday, January 07, 2004

Actually, the C++ standard allows const static members to be initialized in the class declaration, but non-const static members are just like global variables, and they have to follow the "one-definition rule".  Having the definition in the class means that there are more than one file in which that variable is defined, which causes problems for the linker, as it would have to pick which definition you really want.

Ben Combee
Wednesday, January 07, 2004

Tried putting a global in a header file recently?

Mr Jack
Wednesday, January 07, 2004

>> Tried putting a global in a header file recently?

Hmm. Right...

Sorry didn't mention it was C++. Looks like everyone is going C# these days.

As a side question, what proportion of *home* users have the .NET runtime today?

Alex.ro
Wednesday, January 07, 2004

1%, if you're lucky. :-p

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, January 07, 2004

Static variables must be placed in a specific compilation unit - an actual source file. Declaring a "const int" variable is more like having a #define (well, sort of).

igor
Wednesday, January 07, 2004

"Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope. "

check out msdn (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_27.asp)

HTH
maaakri

maaakri
Sunday, June 06, 2004

*  Recent Topics

*  Fog Creek Home