Array question
-
Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example:
int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ }
andif((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; }
From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks -
Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example:
int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ }
andif((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; }
From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanksjw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of
Scope
ofvariable count
.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse thecount
variable created inmemory
and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it inglobal
or make itlocal to class
which handlling you onmousemove function rather declarign it in function.
"I Think this Will Help" [Vote One Here,.....]
visit me at http://www.thisisalok.tk
-
jw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of
Scope
ofvariable count
.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse thecount
variable created inmemory
and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it inglobal
or make itlocal to class
which handlling you onmousemove function rather declarign it in function.
"I Think this Will Help" [Vote One Here,.....]
visit me at http://www.thisisalok.tk
ThatsAlok wrote: now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function Or ... let it where it is now, but declare it as
static
. By the "lifetime" point of view, global orstatic
is mostly the same. Declare it as class member will let every calss instance hace its own. That is: if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. If you want a same single counter to count everything in all instances, declare it asstatic
. In the function if only that function use it; In the class if many function class use it; globally if you have to access it from everywhere. 2 bugs found. > recompile ... 65534 bugs found. :doh: -
ThatsAlok wrote: now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function Or ... let it where it is now, but declare it as
static
. By the "lifetime" point of view, global orstatic
is mostly the same. Declare it as class member will let every calss instance hace its own. That is: if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. If you want a same single counter to count everything in all instances, declare it asstatic
. In the function if only that function use it; In the class if many function class use it; globally if you have to access it from everywhere. 2 bugs found. > recompile ... 65534 bugs found. :doh:emilio_grv wrote: : if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. i think, it just a beginner problem,so i have given beginner answer. conecpt of
static
is little complicated for beginner.
"I Think this Will Help" [Vote One Here,.....]
visit me at http://www.thisisalok.tk
-
Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example:
int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ }
andif((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; }
From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. ThanksHello, I don't understand your problem exactly, but if this declaration is inside a funktion, well you declare a variable and reset it to zero, then ++ it. could you please try using 'static int ... ' instead of your currnt statement? does it solve any problem?
-
jw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of
Scope
ofvariable count
.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse thecount
variable created inmemory
and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it inglobal
or make itlocal to class
which handlling you onmousemove function rather declarign it in function.
"I Think this Will Help" [Vote One Here,.....]
visit me at http://www.thisisalok.tk
-
Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example:
int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ }
andif((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; }
From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks