Hello Forums,
i´m starting to interfaces in C++ on a stm32f3 under µVision4. To start of i created an interface as simple as possible
#ifndef _ICNLSUPPL_H_ #define _ICNLSUPPL_H_ class IChnlSupplyerr { public: IChnlSupplyerr(){}; ~IChnlSupplyerr(){}; virtual signed short doIt(signed short arg) = 0; }; #endif
I have made two classes, which implementing the "doIt" method. One simply adds 3 and the other subtracts 2. In a third class i instantiate these classes and store pointer of each in an array of the Interfaces type.
#include "Chnl.h" Chnl::Chnl() { objArr[0] = &add3; objArr[1] = &sub2; } signed short Chnl::Run(unsigned short sel, signed short arg) { if(sel == 0) arg = objArr[0] -> doIt(arg); if(sel == 1) arg = objArr[1] -> doIt(arg); return arg; }
The wired thing is, if i instantiate an object of the "Chnl" class directly in the main file, everything works as expected. But if i do the same thing in an object below the main, i can track witch the debugger that the hard fault handler is called when it commes to if(sel == 0) arg = objArr[0] -> doIt(arg); After some trying "this and that" i think, that the adresses change somehow from the constructor until the acctual call. I wrote an "If" comparing like If(objArr[0] == &add3) "switch on led".
Has someone an idea, what i´mdoing wrong?
Memory overwrite?
Stack overflow?
Have you tried to trace what happens to the pointers to your objects as you single-step the program - when does the 'this' pointer get corrupted?
Hm... It works if i instatiate an Objekt of the Chnl class at the top of the .cpp file instead of instantiating it in the .h file. Sadly from my studys i don´t have an "in depth" view to compilers to figure out what the difference is...