Static methods do exist in C++. The usage rules are identical.
A static method may not access any instance data so it can only work on
variables that are (a) handed to it as a parameter or (b) entirely within
the scope of the method, ie on the stack or (c) static members of the same
or some other class.
An instance method is one which requires an instance of a class to exist
before it can run. Instance data is the non-static data in a class and each
instance of the class will have different instances of the data.
Example..
public class foo
{
static int anInt;
int anOtherInt;
void doo()
{
int x=poo(10); //VALID instance methods may call static methods
anInt=20; // VALID instance methods may access static data
}
static int poo(int woo)
{
anInt=woo; //VALID a static method may access static data
anOtherInt=woo; //EROR a static method cannot accesss instance data
doo(); //ERROR a static method may not access instance methods
int local=20;
local*=10; //VALID a static method may access variables within its
scope
}
}
The private keyword refers to access or visibility of members and classes
and bears no relation to the static keyword.
A private data member or method may only be accessed from the declaring
class. Derived classes may not access the data or member.
--
Bob Powell [MVP]
Visual C#, System.Drawing
The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed:
http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS:
http://www.bobpowell.net/tipstricks.xml
Bob's Blog:
http://bobpowelldotnet.blogspot.com/atom.xml
"Jay" <j@h.com> wrote in message news:44VFc.1733$r3.360@okepread03...[color=blue]
> Why are there static methods in C#. In C++ static was applied to data[/color]
only[color=blue]
> (I believe) and it meant that the static piece of data was not a part of[/color]
the[color=blue]
> object but only a part of the class (one copy of data as opposed to[/color]
multiple[color=blue]
> instances of the class)
>
> C# is now applying the static concept to methods. Why?, I thought that[/color]
only[color=blue]
> one copy of the methods were loaded into memory not matter how many
> instances were created. Is this different in C#? A static method seems[/color]
to[color=blue]
> mean that the method cannot access any variable or method outside of the
> object.
>
> When static is applied to data, if methods access the private data, the[/color]
data[color=blue]
> is required to be static.
>
> Please explain Static vs Private. Thank You all so much!
>
> Have a happy 4th!
>
>[/color]