I find it worth adding that the static variable does not match the constant variable.
using constant variable in class
struct Foo{ const int a; Foo(int b) : a(b){} }
and we will declare it as if
fooA = new Foo(5); fooB = new Foo(10);
For a static variable
struct Bar{ static int a; Foo(int b){ a = b; } } Bar::a = 0;
which is used like that
barA = new Bar(5); barB = new Bar(10);
You see what is happening here. The constant variable that is initialized with each instance of Foo, since Foo has a value, has a separate value for each instance of Foo and cannot be changed at all by Foo.
Where, as in the case of Bar, this is only one value for Bar :: regardless of the number of instances of Bar. They all share this value, you can also access it, since they are instances of Bar. The static variable also supports the rules for public / private, so you can make it so that only instances of Bar can read the value of Bar :: a;
thecoshman Feb 16 '11 at 17:44 2011-02-16 17:44
source share