How does static work?

if you can put it in dummy language, i am a dummy after all,

I have just been trying to call a class from into my main.cpp

the header file contained the deceleration, and the .cpp file to go with the header contained the definition

1
2
3
4
5
6
7
//header deceleration
class Screen
{
public:
	void Draw ();
	int StartScreen();
};


1
2
3
4
5
6
7
8
9
10
//the .cpp to go with the header
int Screen::StartScreen()
{
		glClearColor(1.f, 0.f, 0.f, 1.f);
	glClear (GL_COLOR_BUFFER_BIT); //Clear the colour buffer 
	glLoadIdentity();    
//Draw everything we've done on the screen	
	glFlush();
	return 0;
}

/
and me asking to use this function in the main..
1
2
3
4
5
/the main.cpp where i want to call classes so to speak..
void Display ()
{
	Screen.StartScreen();
}


only after i made int StartScreen(); static int StartScreen(); and changed Screen.StartScreen(); to
Screen::StartScreen();

the code now works, but I'm lost on how to do things, before i was getting the following error:
main.cpp(10): error C2143: syntax error : missing ';' before '.'

this doesn't make a lot of sense to me, from what i can remember when i was reading about classes the way to make a 'call' so to speak to a class asset is with class.asset not class::asset, unless i was defining the asset, anybody able to why this works and why the other doesn't and is their a better way to do this? Obviously on my journey through C++ classes will be used so much, i need to understand them.

EDIT: Cleaned it up a bit so I've put a tittle of each code.
Last edited on
It is not class.asset, it is object.asset.

Classes represent an outline for a "thing". Objects are instances of that thing.

For example, std::string is a class. You can have multiple strings by declaring them:

1
2
3
// a and b are two different strings
string a;
string b;


Here, a and b are "objects", the class is string.

Now, string has a member function, length, which gets the length. Proper usage is something like this:

1
2
int foo = a.length();  // get the length of our 'a' string.
foo = b.length();  // get the length of our 'b' string. 


Note that before the dot (.) we put the object that we want to act on. We do not put the class name. For example:

1
2
foo = string.length(); // this makes no sense.  What string are we getting the length of?
  // a?  b?  some other string? 


Your Screen::StartScreen class has the same problem. There could be any number of screens:

1
2
3
4
5
6
// for example, say you had 2 screens:
Screen titlescreen;
Screen gamescreen;

// now, before, you were doing this
Screen.StartScreen();  // this makes no sense.  Which screen are you starting? 



This is why you were getting an error.

What static does in this case, is it makes a single function that is shared by ALL objects of the class. That way you don't call it with any single object. Therefore you use the Class::Function syntax:

1
2
3
4
5
// assuming StartScreen is static:
Screen::StartScreen();

// the above is similar in concept to this:
all_screens_in_your_program.StartScreen();


While this technically works the way you have it coded, you'll notice it doesn't make a whole lot of sense conceptually. Why and how would you draw all the screens in one function?

You probably don't want to use static here. Instead you would want to create an instance/object of your Screen class and call StartScreen with that object.
Last edited on
OK so just to clarify, I'm probably wrong about this but, an instance of a class does not exist until i make it? Screen Make_A_Screen_Instance The information is just waiting to be given a name and started (making an instance of it/making Make_A_Screen_Instance an object?)?

I understand more about it now, just want to make sure i'm getting the right picture.
Last edited on
Yes, that is correct.
Topic archived. No new replies allowed.