• Create Account

### #ActualCornstalks

Posted 07 October 2012 - 11:01 PM

A static method has access to all parts of the class. What it doesn't have is a this' pointer, but if it handles any instances of the class, it has full access to those instances, not only through the public interface. Think of the dot' example that Lauris posted above.

Ah, I was just talking about basic (non-static) variables declared in the class.
Like in the following example:
[...]

You missed alvaro's point. Consider the following:

#include <iostream> // this is C++... let's use C++, not C
class MyClass
{
private:
int var1;
static int var2;
public:
MyClass()
{
var1 = 1;
}

static void printVar()
{
MyClass mc;
std::cout << "var1: " << mc.var1 << std::endl; // Look, I can access private members!
std::cout << "var2: " << var2 << std::endl;
}
};

int MyClass::var2 = 2;

int main()
{
MyClass::printVar();
}


I'm sorry to further digress from C#, but I feel like this should be further expanded for future readers. The following is C/C++ specific, and the majority of what other people before me have said apples to C#. Bregma pointed out an important effect of the static keyword in C++: "it can be used to indicate to the linker that a symbol is local only to the translation unit." People so far have talked about variables and class member methods, but no one has talked about global/non-class methods. But it also applies to global methods. Consider:
// there is a static modifier on foo, so its symbol exists only in the current source file
// (i.e. you can't call it from another source file, even if you put "extern void foo();" in that
// source file)
static void foo()
{
}

// there is no static modifier for bar, so its symbol is visible from other translation units
// (i.e. in another source file, you can do "extern void bar();" and then call bar())
void bar()
{
}

A further implication of this is that if you have two functions, both named foo(), that do completely different things and are just "helper" functions for a particular source file, you should declare them both "static" or else their symbols will conflict with each other, even though they're not in the same source file! Moral of the story: always make global/non-member functions static, unless you explicitly want them to be available in other translation units (and expect its name to be unique by all who consume your code). This is one aspect where namespaces are so useful.

Sorry for the digression. Now I feel like the discussion on static in C++ is complete, and hopefully we can all get back to focusing on C# and the OP.

### #2Cornstalks

Posted 07 October 2012 - 10:53 PM

A static method has access to all parts of the class. What it doesn't have is a this' pointer, but if it handles any instances of the class, it has full access to those instances, not only through the public interface. Think of the dot' example that Lauris posted above.

Ah, I was just talking about basic (non-static) variables declared in the class.
Like in the following example:
[...]

You missed alvaro's point. Consider the following:

#include <iostream> // this is C++... let's use C++, not C
class MyClass
{
private:
int var1;
static int var2;
public:
MyClass()
{
var1 = 1;
}

static void printVar()
{
MyClass mc;
std::cout << "var1: " << mc.var1 << std::endl; // Look, I can access private members!
std::cout << "var2: " << var2 << std::endl;
}
};

int MyClass::var2 = 2;

int main()
{
MyClass::printVar();
}


I'm sorry to further digress from C#, but I feel like this should be further expanded for future readers. Bregma pointed out an important effect of the static keyword in C++: "it can be used to indicate to the linker that a symbol is local only to the translation unit." People so far have talked about variables and class member methods, but no one has talked about global/non-class methods. But it also applies to global methods. Consider:
// there is a static modifier on foo, so its symbol exists only in the current source file
// (i.e. you can't call it from another source file, even if you put "extern void foo();" in that
// source file)
static void foo()
{
}

// there is no static modifier for bar, so its symbol is visible from other translation units
// (i.e. in another source file, you can do "extern void bar();" and then call bar())
void bar()
{
}

A further implication of this is that if you have two functions, both named foo(), that do completely different things and are just "helper" functions for a particular source file, you should declare them both "static" or else their symbols will conflict with each other, even though they're not in the same source file! Moral of the story: always make global/non-member functions static, unless you explicitly want them to be available in other translation units (and expect its name to be unique by all who consume your code). This is one aspect where namespaces are so useful.

### #1Cornstalks

Posted 07 October 2012 - 10:49 PM

A static method has access to all parts of the class. What it doesn't have is a this' pointer, but if it handles any instances of the class, it has full access to those instances, not only through the public interface. Think of the dot' example that Lauris posted above.

Ah, I was just talking about basic (non-static) variables declared in the class.
Like in the following example:
[...]

You missed alvaro's point. Consider the following:

#include <iostream> // this is C++... let's use C++, not C
class MyClass
{
private:
int var1;
static int var2;
public:
MyClass()
{
var1 = 1;
}

static void printVar()
{
MyClass mc;
std::cout << "var1: " << mc.var1 << std::endl; // Look, I can access private members!
std::cout << "var2: " << var2 << std::endl;
}
};

int MyClass::var2 = 2;

int main()
{
MyClass::printVar();
}


I'm sorry to further digress from C#, but I feel like this should be further expanded for future readers. Bregma pointed out an important effect of the static keyword in C++: "it can be used to indicate to the linker that a symbol is local only to the translation unit." People so far have talked about variables and class member methods, but no one has talked about global/non-class methods. But it also applies to global methods. Consider:
// there is a static modifier on foo, so its symbol exists only in the current source file
// (i.e. you can't call it from another source file, even if you put "extern void foo();" in that
// source file)
static void foo()
{
}

// there is no static modifier for bar, so its symbol is visible from other translation units
// (i.e. in another source file, you can do "extern void bar();" and then call bar())
void bar()
{
}

A further implication of this is that if you have two functions, both named foo(), that do completely different things and are just "helper" functions for a particular source file, you should declare them both "static" or else their symbols will conflict with each other, even though they're not in the same source file! Moral of the story: always make global/non-member functions static, unless you explicitly want them to be available in other translation units (and expect its name to be unique by all who consume your code).

PARTNERS