Still confused about "static"

Started by
14 comments, last by kuramayoko10 11 years, 6 months ago
Am I the only one seeing the C# tag on this thread? (This is not a rhetorical question, I'm severely confused by the number of C++ specific responses here.)
Advertisement

Am I the only one seeing the C# tag on this thread? (This is not a rhetorical question, I'm severely confused by the number of C++ specific responses here.)


Nope, I see it too.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Am I the only one seeing the C# tag on this thread? (This is not a rhetorical question, I'm severely confused by the number of C++ specific responses here.)

I carefully ignored it. I apologize for any confusion I might have created: I know nothing about C#.

Am I the only one seeing the C# tag on this thread? (This is not a rhetorical question, I'm severely confused by the number of C++ specific responses here.)


There is no C# "prefix" so I guess most people - like me - don't really look at the tags and just assume that every question is C++ unless the OP mentions another language in their question, especially when the topic is something that is essentially the same in most languages anyway.

[quote name='alvaro' timestamp='1349633597' post='4987729']
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:
[...]
[/quote]
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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
@Cornstalks
Thank you por pointing that out and for complementing this topic.

Sorry for the C++ code as well.
Programming is an art. Game programming is a masterpiece!

This topic is closed to new replies.

Advertisement