Re: Help!!! I give up...Confused!
Robby wrote:
Hello,
It is against my will to post such a lenghty problem, however... I give
up!....I don't know what is wrong with this program... It must have something
to do with the way my objects are declared which renders my base class to
forget the value of one of its members.
I recently got back into, inheritance, polymorphism and virtual methods. I
really don't need the power of polymorphism right now since I don't need to
create a base class pointer type. I just need to call methods in my classes
while in WndProc!
The program compiles without errors!
Anyhow, I think you would better understand the code posted below....
Again, sorry for the lenghty code... I have tried to cut it down the most I
can while keeping the integrity of the problem as a whole.
=====================================================class components
{
public:
components(){}
virtual ~components() {}
virtual void SYS_set_FLAG_AllTrueInCase();
virtual bool SYS_get_FLAG_AllTrueInCase();
virtual void SYS_SFCC(bool bCondTrue);
private:
//By default this member is set to true before every "switch(message)"
bool FLAG_AllTrueInCase;
};
void components::SYS_set_FLAG_AllTrueInCase()
{
FLAG_AllTrueInCase =true;
}
bool components::SYS_get_FLAG_AllTrueInCase()
{
//Under cursor in debug, at this point, FLAG_AllTrueInCase
//is true??? Didn't we set it to false ???????
return FLAG_AllTrueInCase;
}
void components::SYS_SFCC(bool bCondTrue)
{
//Set FLAG_AllTrueInCase to true if FLAG_AllTrueInCase
//and bCondTrue equals to true else set it to false.
if(FLAG_AllTrueInCase && bCondTrue)
FLAG_AllTrueInCase =true;
else
FLAG_AllTrueInCase =false; //Therefore: FLAG_AllTrueInCase is set to
false...right!
}
//-----------------------------------------------
class transitions:public components
{
public:
transitions(){}
virtual ~transitions(){}
virtual bool SYS_get_FLAG_AllTrueInCase();
virtual void SYS_SFCC(bool bCondTrue);
};
bool transitions::SYS_get_FLAG_AllTrueInCase()
{
if (components::SYS_get_FLAG_AllTrueInCase())
return true;
else
return false;
}
void transitions::SYS_SFCC(bool bCondTrue)
{
components::SYS_SFCC(bCondTrue);
}
//-----------------------------------------------
class T_LOGIC:public transitions
{
public:
T_LOGIC(){}
virtual ~T_LOGIC(){}
virtual void get_BOOL_AND_INPUT (int Bool_ID_1, int Bool_ID_2, IO *io);
protected:
};
void T_LOGIC::get_BOOL_AND_INPUT(int Bool_ID_1, int Bool_ID_2, IO *io)
{
//Gets the actual value of input 1 and 2. If they are both true! then...
else....
//As I run the program, input 1 and 2 are false!!!!! so then (else is
fetched!)
if (io->getBOOL_INPUT_IMT(Bool_ID_1) && io->getBOOL_INPUT_IMT(Bool_ID_2))
transitions::SYS_SFCC(true);
else
transitions::SYS_SFCC(false);
}
//-----------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static ENVIR envir;
static IO io;
transitions *pT_TRANSITION = new transitions;
T_LOGIC *pT_LOGIC = new T_LOGIC;
switch(message)
{
case WM_TIMER:
//Upon the next line, the component's member
//FLAG_AllTrueInCase will be set to false!
pT_LOGIC->get_BOOL_AND_INPUT(2,3,&io);
//Now lets check for the value of FLAG_AllTrueInCase
//It Should return a false!!!! But it returns a true... confused!
if(pT_TRANSITION->SYS_get_FLAG_AllTrueInCase())
{}
....other code.....
delete pT_TRANSITION;
delete pT_LOGIC;
...other code.....
=====================================================
In short... What must I do to keep the component's member value as static. I
tried to make it static, but the compiler gives an error!
All suggestions very appreciated....
I feel very bad for this and above all, confused!
Hope to hear from you all!
Robby:
1. You have a non-static member variable FLAG_AllTrueInCase that is not
initialized in the constructor. This is an absolute no-no. Thw whole
purpose of the cosntructor is to initialize the object.
2. I'm not sure what you mean by "keep the component's member value as
static". If you want it to be a static member, then learn how to define
a static member variable of a class (don't just give up). Hint: you have
to define it in the implementation file.
3. Your pT_TRANSITION and pT_LOGIC are pointers to separate objects, so
they have separate values of FLAG_AllTrueInCase.
4. Don't use "new" for local variables. Create them on the stack.
5. Use const modifier on methods that do not change the object (e.g.
your SYS_get_FLAG_AllTrueInCase(). It makes the code so much easier to
understand.
David Wilkinson