hiho@ll
what i have:
on one hand a static method variable within a class
on the other a static class variable within a class
what i thought it should be:
the static class variable depends on the class not on a object instance, this means i could do something like a "instance" counter (i think everybody knows such examples)
the static local variable in my method can only be accessed through my method and it doesn't "die" between 2 function calls, BUT it has nothing to do with static class variables so a instance counter isn't possible because if i create another object the static method variable is initialized newly
so the static method variable is instance bound
code:
Code:
#include <iostream>
using namespace std;
class StaticTest
{
public:
void SetValue(int Value);
int GetValue(void);
private:
int summe;
};
void StaticTest::SetValue(int Value)
{
static int counter = 0;
counter += Value;
summe = counter;
}
int StaticTest::GetValue(void)
{
return summe;
}
int main()
{
//3 Objekte:
StaticTest *a, *b, *c;
a = new StaticTest();
b = new StaticTest();
c = new StaticTest();
//1. Object
a->SetValue(1);
a->SetValue(1);
a->SetValue(1);
cout << "Value of a is " << a->GetValue() << "\n";
//3
delete a;
//2. Object
b->SetValue(1);
//3. Object
c->SetValue(1);
cout << "Value of b is " << b->GetValue() << "\n";
//4
cout << "Value of c is " << c->GetValue() << "\n";
//5
return 0;
}
the output of the above code is:
Value of a is 3
Value of b is 4
Value of c is 5
what i thought it should be:
Value of a is 3
Value of b is 3
Value of c is 3
question:
what is the difference between a local static variable and static class member?
i mean the only difference is (as i see for now), that i can't access the local variable directly
but:
Code:
class{
public:
void mymethod(){
static int counter;
}
private:
static int othercounter;
};
what's the difference in the above code? does it make sense that there is no difference? (well i think there is no difference)
or maybe a local static variable within a method is only a "relict" from C?