AnsweredAssumed Answered

C++ Virtual function with CodeWarrior Development Studio v3.1 doesn't work?

Question asked by Christian Darnell on May 10, 2007
Latest reply on May 11, 2007 by CompilerGuru
Hi all,  
I have a problem using CW dev. studio v3.1 and virtual functions in C++.  When running the code below I will expect the variable i to count from 0 to 4 (I verified this with another C++ compiler). I'm using P&E ICE to debug this.
When the constructor of myClass is running the variable i is initialized to 0, and it works fine.
But when the Run() method in myClass is running for the first time one can see that i isn't zero and that is isn't 1 after the first run.
I also noticed that the this pointer changes every time the debugger stops at my break point in the Run() method, and it should be the same all four times, right?
Any ides what might be wrong?
I'm using a GB60 processor (M68EVB908GB60 evaluation board) and the following dev. studio is installed:
Installed Products:
CodeWarrior Development Studio for Motorola HC08 3.1, build 4210.       
CodeWarrior Development Studio for Motorola HC08 3.1 P&E USB 2.0 Service Pack build 050114.
CodeWarrior Development Studio for Freescale HC08 v3.1 Service Pack GB/GT 60A, build 050711
CodeWarrior Development Studio for Freescale HC08 v3.1 Service Pack Programming Algorithms, build 060221
#include <hidef.h> /* for EnableInterrupts macro */#include <MC9S08GB60.h> /* include peripheral declarations */class Runnable{ public:   Runnable();   ~Runnable();   virtual void Run(void) = 0;}; Runnable::Runnable() { } Runnable::~Runnable() { }class myClass : public Runnable{ public: myClass(); ~myClass(); void Run(void); private: unsigned int i;};myClass::myClass() :i(0){}myClass::~myClass(){}void myClass::Run(void){ i = i + 1;}void main(void) { EnableInterrupts; /* enable interrupts */ /* include your code here */ //Disable watchdog _SOPT.Bits.COPE = 0; Runnable *myRun[2]; myRun[0] = new myClass();       myRun[0]->Run();       myRun[0]->Run();       myRun[0]->Run();       myRun[0]->Run();       for(;;) {   //__RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave this function */}
Best Regards,

Message Edited by SW Designer on 2007-05-10 09:46 PM