16th Jul 2020, 7:18 PM
Hilary
Hilary - avatar
10 Respostas
+ 4
simple example: #include <iostream> using namespace std; int increase(){ static int a; //automatically initialise to 0; a++; return a; } int main(){ cout << increase() << endl; // output is 1 cout << increase() << endl; // output is 2 cout << increase() << endl; // output is 3 return 0; } .....think of it as a global variable but is only accessible from within the function in which it was declared.
16th Jul 2020, 8:05 PM
rodwynnejones
rodwynnejones - avatar
+ 2
benjamin , What about in a local variable like "int static I = 5", what will happen in the case of the CPP code I've linked?
16th Jul 2020, 7:31 PM
Hilary
Hilary - avatar
+ 2
Yea. But from what I've learned, "static" is not a beginner's favorite word. 😂😂😂
16th Jul 2020, 7:40 PM
Hilary
Hilary - avatar
+ 1
A static class cannot have objects created from it. It is accessed from it's own name, not from the name of objects made from it.
16th Jul 2020, 7:27 PM
benjamin
+ 1
I've just read your code snippet. I can't see why you'd want to do that, but if you have a static local variable then it's value is shared across all instances of a class. So any changes you make to it in one object would affect the value in the other object. Of course, if the class is static then you wouldn't have these objects anyway.
16th Jul 2020, 7:32 PM
benjamin
+ 1
😓 it's a bit confusing because I haven't studied much in CPP. But am getting the hang of it.
16th Jul 2020, 7:35 PM
Hilary
Hilary - avatar
+ 1
😂😂😂 add it to the list...
16th Jul 2020, 7:41 PM
benjamin
0
I don't really do cpp either but the concepts are generally the same across most languages. I hope I cleared it up a little bit for you. Just keep practicing, keep learning and more and more will begin to make sense.
16th Jul 2020, 7:37 PM
benjamin
0
Static members shared memory on server
18th Jul 2020, 2:08 PM
Jahanzaib Shahid
Jahanzaib Shahid - avatar