Olá,
Estou escrevendo um projeto em C++. Preciso utilizar atributos de classe (estáticos). Algumas classes do Projeto utilizam funções estáticas sem a necessidade de atributos estáticos. Isso funciona corretamente. Porém, quando precisei criar um servico de tempo e adicionar atributos de estáticos na classe, o codigo não funciona na placa. Contudo, eu testei na simulação e funcionou sem problemas. Além disso, existem outras classes no mesmo projeto que também utilizam atributos estáticos e até agora não tive problemas. Segue o código .h e .cpp.
Hello,
I'm writing a project in C ++. I need to use class attributes (static). Some classes of the Project use static functions without the need for static attributes and this works correctly. However, when I needed to create a service time and add static attributes in this class, the code did not work on the board. However, I have tested in simulation and worked without problems. In addition, there are other classes in the same project that also use static attributes and so far had no problems. Here is the code .h and .cpp
//---------------- Header File (TimeService.h) namespace service { class TimeService : public Service{ public: TimeService(hal::Timer* timer); static void wait(U32 miliseconds); virtual void init(); private: static hal::Timer* timer; static U32 timeCount; }; } //----------------- End Header File (TimeService.h) //---------------- CPP File (TimeService.cpp) hal::Timer* service::TimeService::timer; U32 service::TimeService::timeCount; namespace service { TimeService::TimeService(hal::Timer* timer) { TimeService::timeCount = 0; TimeService::timer = timer; } void TimeService::init() { //DO NOTHING. } void TimeService::wait(U32 miliseconds) { TimeService::timeCount = ((miliseconds* TimeService::timer->getTickInterval()) / 1000); while(TimeService::timeCount > 0); } } //----------------- End CPP File (TimeService.cpp)
When i remove the contents of all functions, including the constructor, the program works. The program always simulate whitout problems.
I'm sorry, I forgot the tick member function:
void TimeService::tick() { if(TimeService::timeCount) { TimeService::timeCount--; } }
However, I cannot understand why this problem happens. Would it be a compiler bug? Is there a limit for static variables declaration? What could be the cause?
Start with a small test program that contains the idea you are trying to use here. Does it still fail? Did you debug your failed program?
The only open issue is when constructors for global objects are called in relation to the initialization of DATA and BSS and the configuration of the stack(s).
But the word "static" for member fields in a class just means that the members will be shared between all objects of that type, and stored as a global variable. And if the object itself is declared static inside a function, it will be stored as an unnamed global object.
What does the base class constructor do?