+ 3
What is purpose of static keyword?
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.
+ 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?
+ 2
Yea. But from what I've learned, "static" is not a beginner's favorite word. đđđ
+ 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.
+ 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.
+ 1
đ it's a bit confusing because I haven't studied much in CPP. But am getting the hang of it.
+ 1
đđđ add it to the list...
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.
0
Static members shared memory on server