My class has a variable of a user-defined type. I set this variable in one method of the class. When I try to get this variable from another method of the same class, the value is always 0, although the variable has not been changed anywhere else.
I really don't understand why this happens! Follwoing is a sample of my code:
myclass.h
typedef enum {
ACTIVE = 0,
SLEEP = 1,
FINISHED = 2,
WAITING = 3,
KILLED = 4
} Mode;
class MyClass
{
public:
void statusReceive(void);
Mode getCurrentMode(void);
private:
Mode currentMode;
};
myclass.cpp
#include "myclass.h"
void MyClass::statusReceive(void)
{
currentMode = (Mode)interpretMsg(&msg);
printf("current Mode = %d\n", this->currentMode); // prints 4
}
Mode MyClass::getCurrentMode(void)
{
printf("current Mode = %d\n", this->currentMode); // prints 0
return this->currentMode;
}
main.cpp
#include "myclass.h"
MyClass myclass;
void timerStart(std::function<void(void)> func, unsigned int interval)
{
std::thread([func, interval]()
{
while (true)
{
auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(interval);
func();
std::this_thread::sleep_until(x);
}
}).detach();
}
int main(void)
{
timerStart(std::bind(&MyClass::statusReceive, myclass), 10);
Mode x = myclass.getCurrentMode();
printf("Current Mode = %d\n", x); // prints 0
}