I have the following class definition in C++ :
class foo {
private:
int a,b,c;
foo(); // constructor
void setup(int x, int y);
void some_function_that_changes_a_b_c();
}
foo::foo() {
a = b = c = 0;
}
void foo::setup(int x, int y) {
foo(); // <-- I use this to make sure a,b,c are initialized
a = x;
b = y; // <-- c is left unchanged!
}
void foo::some_function_that_changes_a_b_c() {
// here's some code that changes variables a,b,c
}
And then I have a code that uses this class:
foo *a = new foo;
a->setup(1,2);
a->some_function_that_changes_a_b_c();
a->setup(5,7); // <-- HERE IS THE PROBLEM
The problem is that on the second call to setup(), it doesnt run the foo() constructor to reset my values or a,b,c , so the c variable stays with the old value it was on the call to some_function_that_changes_a_b_c(), I tested this with the debugger and it seems like on the second call, foo() is addressing a different memory space for the variables.
Is there a way to fix this?