At 04:59 PM 1/7/00 +0000, you wrote:
>I think you will find that static has a slightly different meaning. If you
>wish to limit a procedures scope to the current Module/Class/Source File
>then the procedure need only be defined as Private. Static means that the
>code persists even when the module is out of scope. This holds true for
>variable definitions. A variable defined within a module or procedure will
>persist through to the next call of the module or procedure. I am not so
>sure with procedures. It would make sense for static to mean that the
>procedures code remains in memory rather then being released when the module
>is released. Can anyone verify this to be the correct behaviour for a static
>procedure.

You are correct.  I was thinking of the case in which "static" is used to
modify a function that is not part of a class.
In a C++ class, the "static" keyword implies that the method or attribute
has class scope - that is it is the same for all instances of a class.  

For example, if I have a class MyClass:

class MyClass
{
private:
   static Char s_className [50];  // class variable.
   Char m_instanceName [50];   // instance variable.

public:
   // Constructor for class.
   MyClass (CharPtr name) { StrCopy (m_instanceName, name)

   // Class function
   static CharPtr GetClassName() { return s_className; }

   // Instance function.
   CharPtr GetInstanceName() { return m_instanceName; }
};

Somewhere in my source files I'll need to instantiate the class variable,
s_className.  Note that here I *do not* declare the variable scope to be
static, because that would imply 'C' style static (ie, limited to the
source module).

Char MyClass::s_className [50] = "This is my class";

And let's say I have a function that uses this class:

void DoSomethingWithMyClass ( )
{
   MyClass instanceOfMyClass ("An Instance of My Class");
   CharPtr nameP;
   CharPtr anotherNameP;
   CharPtr yetAnotherNameP;

   // I can access public class methods and attributes via the class itself.
   // This will return MyClass::s_className.
   nameP = MyClass::GetClassName ();

   // I can also access public class methods via an instance of the class.
   // This will also return MyClass::s_className.  
   // So nameP == anotherNameP
   anotherNameP = instanceOfMyClass.GetClassName ();

   // I can even create another instance and access the class method.
   // This will return MyClass::s_className same as previous two invocations.
   // So nameP == anotherNameP == yetAnotherNameP
   MyClass anotherInstanceOfMyClass ("Another Instance of My Class");
   yetAnotherNameP = anotherInstanceOfMyClass.GetClassName();

   // But I can only access public instance methods via an instance of the
class.
   nameP = instanceOfMyClass.GetInstanceName ();

   // And if I access the instance method of the other instance, I get a
different value.
   // So nameP != anotherNameP.
   anotherNameP = anotherInstanceOfMyClass.GetInstanceName ();

   // The following call is illegal and would not compile.
   nameP = MyClass::GetInstanceName ();   // Need a specific instance to
invoke method.
}

So basically what the static is doing is to declare the method or attribute
as being of class scope, which means that the same method/attribute is
available for all instances of the class.

I could go on for hours about this, but I think I've rambled long enough.
If anyone is interested in more nuances of the "static" keyword in C and
C++, feel free to e-mail me off list.  I would also strongly recommend
Scott Myer's "Effective C++" as a great reference for saying what you mean
in C++.

Hope this helps.

Greg


Greg Winton
Bachmann Software and Services, LLC
http://www.bachmannsoftware.com
Software for Handheld & Wireless Computing
Authors of "Palm Programming", published by Macmillan/Sams
Home of Bachmann Print Manager, the only graphical printing solution for
the Palm
Computing Platform

Reply via email to