For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > Please refer to Post "Help!!! I give up... Confused"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Please refer to Post "Help!!! I give up... Confused"
Robby

2006-05-25, 7:08 pm

Hello,

Please refer to my previous post called "Help!!! I give up...Confused" that
I posted on May-24-2006.

I am sorry, I could not post another question in that post because the
system won't let me.. I have problem viewing and replying to my posts, I
don't know why this is happening!

Anyhow... By the way, thankyou David and all others that answered my
previous post.

Yes David, I did look into a few chapters ahead in my book, it was a little
rough but I figured it out on how to declare static member variables in a
class... anyhow I will re-show how I did this... and to my surprise, it
works! I had gotten so disappointed because, I was trying and trying.... and
I was always getting the wrong result.... It was so frustrating that it
caused me to say "I give up"... But no worry, If I said that I would give up,
I didn't really mean it, it was only out of frustration! Anyhow I thankyou
for you encouragement, sometimes we all need it!

I have a few questions about what you suggested in my previous post, but for
now
here's what I did. (I don't know if this is the best way to do it, but I
followed the book example!)
========================================
==========
components
{
public:
components(){}
virtual ~components() {}
virtual void SYS_set_FLAG_AllTrueInCase();
virtual bool SYS_get_FLAG_AllTrueInCase();
virtual void SYS_SFCC(bool bCondTrue);
static bool FLAG_AllTrueInCase;

private:
};

//static Member declared
bool components::FLAG_AllTrueInCase;

components::components(bool Flag)
{
FLAG_AllTrueInCase = true; //For now I made it a constant "true"
}

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 now false, just as we set it!
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!

}


//-----------------------------------------------



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;
static components cp(true);

transitions *pT_TRANSITION = new transitions;
T_LOGIC *pT_LOGIC = new T_LOGIC;

switch(message)
{

case WM_TIMER:

//The next line, the component's class member
//FLAG_AllTrueInCase will be set to false!
pT_LOGIC->get_BOOL_AND_INPUT(2,3,&io);

//Now this returns the value of false which what I wanted!
if(pT_TRANSITION->SYS_get_FLAG_AllTrueInCase())
{}

.....other code.....

delete pT_TRANSITION;
delete pT_LOGIC;

....other code.....

========================================
=============

Okay, inspite of other suggestions I got from other newsgroups collegues in
my previous post, such as create a seperate class outside WndProc to store
the value of "FLAG_AllTrueInCase", well! for now... I am choosing to stay
with doing it in WndProc even though it means that I am re-creating it
everytime WndProc is executed.

A few questions:

Now... when I create an object of TRANSITION and LOGIC types, I guess that I
am creating 2 objects on the heap where pT_TRANSITION and pT_LOGIC contains
their address. Right... Okay!

So if I create an object of components, TRANSITION and LOGIC types, then
what am I doing exactly? I mean, does the variable of "FLAG_AllTrueInCase"
exit three times (one per every object that I create) and furthermore all my
methods pertaning to TRANSITION, do they also exist in the LOGIC object and
in the components object?

The reason why I ask is because evrytime I create these objects at the same
time, then copies of methods and data members will exist at the same time...
isn't this ineffecient?
Just asking!

As far as using constant keyword for my methods that don't change any data
in my classe's members, yes,yes... I will do this later on in my code, for
now there was just some prioritary design tasks I had to understand!

And last but not least, is it Okay for me to use "new", I feel very
comfortable in creating my objects on the heap... Is it mandatory to create
them on the stack! or is it a matter of preference.

Although this post's feedback is geared towards David's reply and
suggestions, anyone can get back to me.

David and everyone else, I thankyou very much!

--
Best regards
Robert
David Wilkinson

2006-05-26, 7:09 pm

Robby wrote:

> Hello,
>
> Please refer to my previous post called "Help!!! I give up...Confused" that
> I posted on May-24-2006.
>
> I am sorry, I could not post another question in that post because the
> system won't let me.. I have problem viewing and replying to my posts, I
> don't know why this is happening!
>
> Anyhow... By the way, thankyou David and all others that answered my
> previous post.
>
> Yes David, I did look into a few chapters ahead in my book, it was a little
> rough but I figured it out on how to declare static member variables in a
> class... anyhow I will re-show how I did this... and to my surprise, it
> works! I had gotten so disappointed because, I was trying and trying.... and
> I was always getting the wrong result.... It was so frustrating that it
> caused me to say "I give up"... But no worry, If I said that I would give up,
> I didn't really mean it, it was only out of frustration! Anyhow I thankyou
> for you encouragement, sometimes we all need it!
>
> I have a few questions about what you suggested in my previous post, but for
> now
> here's what I did. (I don't know if this is the best way to do it, but I
> followed the book example!)
> ========================================
==========
> components
> {
> public:
> components(){}
> virtual ~components() {}
> virtual void SYS_set_FLAG_AllTrueInCase();
> virtual bool SYS_get_FLAG_AllTrueInCase();
> virtual void SYS_SFCC(bool bCondTrue);
> static bool FLAG_AllTrueInCase;
>
> private:
> };
>
> //static Member declared
> bool components::FLAG_AllTrueInCase;
>
> components::components(bool Flag)
> {
> FLAG_AllTrueInCase = true; //For now I made it a constant "true"
> }
>
> 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 now false, just as we set it!
> 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!
>
> }
>
>
> //-----------------------------------------------
>
>
>
> 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;
> static components cp(true);
>
> transitions *pT_TRANSITION = new transitions;
> T_LOGIC *pT_LOGIC = new T_LOGIC;
>
> switch(message)
> {
>
> case WM_TIMER:
>
> //The next line, the component's class member
> //FLAG_AllTrueInCase will be set to false!
> pT_LOGIC->get_BOOL_AND_INPUT(2,3,&io);
>
> //Now this returns the value of false which what I wanted!
> if(pT_TRANSITION->SYS_get_FLAG_AllTrueInCase())
> {}
>
> ....other code.....
>
> delete pT_TRANSITION;
> delete pT_LOGIC;
>
> ...other code.....
>
> ========================================
=============
>
> Okay, inspite of other suggestions I got from other newsgroups collegues in
> my previous post, such as create a seperate class outside WndProc to store
> the value of "FLAG_AllTrueInCase", well! for now... I am choosing to stay
> with doing it in WndProc even though it means that I am re-creating it
> everytime WndProc is executed.
>
> A few questions:
>
> Now... when I create an object of TRANSITION and LOGIC types, I guess that I
> am creating 2 objects on the heap where pT_TRANSITION and pT_LOGIC contains
> their address. Right... Okay!
>
> So if I create an object of components, TRANSITION and LOGIC types, then
> what am I doing exactly? I mean, does the variable of "FLAG_AllTrueInCase"
> exit three times (one per every object that I create) and furthermore all my
> methods pertaning to TRANSITION, do they also exist in the LOGIC object and
> in the components object?
>
> The reason why I ask is because evrytime I create these objects at the same
> time, then copies of methods and data members will exist at the same time...
> isn't this ineffecient?
> Just asking!
>
> As far as using constant keyword for my methods that don't change any data
> in my classe's members, yes,yes... I will do this later on in my code, for
> now there was just some prioritary design tasks I had to understand!
>
> And last but not least, is it Okay for me to use "new", I feel very
> comfortable in creating my objects on the heap... Is it mandatory to create
> them on the stack! or is it a matter of preference.
>
> Although this post's feedback is geared towards David's reply and
> suggestions, anyone can get back to me.
>
> David and everyone else, I thankyou very much!
>


Robby:

This problem with reading posts has to be a problem with your
newsreader, not with the server.

It is good that you have learned how to use static class variables,
though I am not sure that that is what you really want here:

1. Every time you create an object of a class derived from components,
the variable FLAG_AllTrueInCase is set to true (as you have it now).
This behavior could be very surprising and counterintuitive to someone
working with this code. If you want to reset this variable, I think an
explicit call to SYS_set_FLAG_AllTrueInCase() would be better. BTW, this
method should be static, because it only uses a static variable. Like this

transitions *pT_TRANSITION = new transitions;
T_LOGIC *pT_LOGIC = new T_LOGIC;
components::SYS_set_FLAG_AllTrueInCase()
;

2. Your code cannot be as you posted, because class components does not
have a constructor taking a bool argument.

Some other points:

3. Get in the habit of writing const-correct code. Do not postpone it.
Believe me, you will love const-correctness when you get used to it.

4. Really, do not use new unless you have to, especially for variables
that are local to a function:

(a) It requires more lines of code than uisng the stack

(b) It is inefficient (allocating memory is expensive)

(c) You open the possiblity of forgetting to call delete

(d) Your code can leak memory if it throws an exception before reaching
the delete statements. Simple example: there are two uses of new in the
function (as you do here); if the second one fails the memory allocated
in the first will leak.

(e) If you have multiple return points in the function you have to make
sure that the delete is called in all cases. You do not show all your
code, but the different cases in a WndProc normally have their own
return statements. If this is true, your delete statements will never be
called.

(f) It makes you look like a Java programmer :-)

Good luck!

David Wilkinson
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com