[MUD-Dev] Re: PDMud thread summary

Adam Wiggins adam at angel.com
Mon Oct 26 15:24:40 New Zealand Daylight Time 1998


On Mon, 26 Oct 1998, Jo Dillon wrote:
> Nope. Constant variables would be an oxymoron ;) A static variable
> is simply a variable whose value is shared between instances of a class
> (in C++ speak anyway). It's like a global variable but it's in the class's
> scope, which helps with name hiding.

It's slightly more complicated than that, due to C++ being a hack on top
of C.  'static' is best thought of as a way to use global variables with
a limited scope.  For example:

class Foo
{
public:
      static int MyStatic;
};

int Foo::MyStatic = 1;

static int MyStatic = 2;

void bar()
{
    static int MyStatic = 3;
    printf("MyStatic = %d\n", MyStatic);
    {
        static int MyStatic = 4;
        printf("MyStatic = %d\n", MyStatic);
    }
}

main()
{
    printf("Foo::MyStatic = %d\n", Foo::MyStatic);
    printf("MyStatic = %d\n", MyStatic);
    bar();
}

The output will be:

Foo::MyStatic = 1
MyStatic = 2
MyStatic = 3
MyStatic = 4







More information about the MUD-Dev mailing list