Consider below example for global static scope in terms of accessibility
#include <iostream>
using namespace std;
static int y;
class A {
public:
void increment() {
++y;
}
};
class B {
public:
void increment() {
++y;
}
};
int main()
{
A a;
a.increment();
cout << y << endl;
A b;
b.increment();
cout << y << endl;
B c;
c.increment();
cout << y;
return 0;
}
Output
1
2
3
Here global static variable access is within both class A and B.
Consider below example for class static scope in terms of accessibility
#include <iostream>
using namespace std;
class A {
public:
static int y;
void increment() {
++y;
}
};
class B {
public:
static int x;
void increment() {
++x;
}
};
int A::y = 1;
int B::x = 1;
int main()
{
A a;
a.increment();
cout << a.y << endl;
A b;
b.increment();
cout << b.y << endl;
B c;
c.increment();
cout << c.x;
return 0;
}
output
2
3
2
Here static variable y scope is with class A and x scope is with class B.
If you will try to access static variable y with class B object then it will return error. (B b -> b.y)
Life time of both static variable x and y remains till the main ends.
n
non-static I don't believe so. Withn
static probably not in that scope. At global scope there can be. If you're going to ask a question about both languages (which you shouldn't unless your talking about interop or differences between them), at least provide a code sample that is "credible" in both languages. – Undersize