I'm currently stuck on making the title screen to a game I've been planning.
The form loads and TMRopI (a timer) starts ticking, Increasing the opacity of the form, creating a fade in effect, and revealing a logo on the form. every time the timer ticks, an integer increases by 1 (or at least it's supposed to). A picturebox should become visible after the integer reaches 150 (the form is at full opacity when the integer equals 100) creating a pause before the picture is changed. The only problem is, it seems to be comepleteley ignoring my integer.
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
TMRopI->Enabled=true;
}
private: System::Void TMRopI_Tick(System::Object^ sender, System::EventArgs^ e) {
int num=0;
num+=1;
this->Opacity+=0.01;
if (num >= 150)
PBXtitle->Visible=true;
}
Another option is to change the definition of num to static. However, making num a member variable of the class is the more C++ way to do it.
static int num=0;
num+=1;
this->Opacity+=0.01;
if (num >= 150)
PBXtitle->Visible=true;
This way, num gets set to 0 on the first call to the function and it's value is saved between subsequent calls.
int num=0
sets num to 0 every time, does it not?
As written, num will always be 1 in the TMRopI_Tick method. You should declare it in the calling method and pass it as a parameter by reference. Or, as #Matt points out in the comment, declare it as a member of the class.
Edit Your current declaration of num creates it as a local variable. It is "created" each time the method is called and "destroyed" as soon as the method ends. So every time the method is called it starts over at zero and then gets incremented to one. If TMRopI_Tick is called repeatedly inside a loop in another method, for example, you could define num in that calling method as a local variable. Then it would exist for the duration of that method. Then if you passed it to TMRopI_Tick (by reference), it would then be incremented and its scope (think of it as its "life") would exist throughout (and beyond) each call.
Related
I am quite confused how this assembly code works. I have tried looking around for answers but couldn't find anything. I think ldarg.0 is loaded due to an instance void, but i'm not sure why it loads ldarg.1.
Would really appreciate it if someone could explain what's going on.
.method public hidebysig specialname instance void set_phase(string value)
{
.maxstack 3
.locals init (bool V0)
nop
ldarg.1
ldarg.0
ldfld string KGER.Generate::_phase
call bool [mscorlib]System.String::op_Inequality(string, string)
ldc.i4.0
ceq
stloc.0
ldloc.0
brtrue.s loc_4E8
Thanks in advance!
Your code is not complete, however: the portion does the following:
.method public hidebysig specialname instance void set_phase(string value)
{
.maxstack 3
.locals init (bool V0)
This is the method signature. from here you infer two important things: the first is that the method is an instance method. This means that the first implicit argument contains this.
The second important thing is the signature which consists of a single argument of type string: this will be arg1 as arg0 is implicitly used to contain this.
nop
computationally, this does nothing. among other things, nop instructions can be used by debuggers to safely place break points
ldarg.1
this loads arg1 onto the stack. The stack contains (the value of the field named value)
ldarg.0
ldfld string KGER.Generate::_phase
then loads the this argument and immediately uses it to load the KGER.Generate::_phase field. the stack now contains (value, the content of the _phase field)
call bool [mscorlib]System.String::op_Inequality(string, string)
this calls the operator op_Inequality of the class String which is a static method. the stack now contains (result of comparison)
ldc.i4.0
this loads 0 as integer into the stack. as we know that this value will be part of a boolean comparison, remember that 0 is equivalent to false for these purposes
ceq
this compares the two values into the stack and pushes the result as boolean into the stack
stloc.0
this stores the result of the comparison to a local variable (the first one)
ldloc.0
this loads the result of the comparison stored to the abovementioned local variable again into the stack, presumably this is a debug version, and this pair of instructions allows the developer to correctly view the value of the variables while debugging (or it is really needed in a part of code you didn't share)
brtrue.s loc_4E8
this jumps to the location loc_4E8 when the value is true (i.e. 1), which means that if thee two strings are equal, the code will jump.
Could you please explain differences between and definition of call by value, call by reference, call by name and call by need?
Call by value
Call-by-value evaluation is the most common evaluation strategy, used in languages as different as C and Scheme. In call-by-value, the argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function (frequently by copying the value into a new memory region). If the function or procedure is able to assign values to its parameters, only its local copy is assigned — that is, anything passed into a function call is unchanged in the caller's scope when the function returns.
Call by reference
In call-by-reference evaluation (also referred to as pass-by-reference), a function receives an implicit reference to a variable used as argument, rather than a copy of its value. This typically means that the function can modify (i.e. assign to) the variable used as argument—something that will be seen by its caller. Call-by-reference can therefore be used to provide an additional channel of communication between the called function and the calling function. A call-by-reference language makes it more difficult for a programmer to track the effects of a function call, and may introduce subtle bugs.
differences
call by value example
If data is passed by value, the data is copied from the variable used in for example main() to a variable used by the function. So if the data passed (that is stored in the function variable) is modified inside the function, the value is only changed in the variable used inside the function. Let’s take a look at a call by value example:
#include <stdio.h>
void call_by_value(int x) {
printf("Inside call_by_value x = %d before adding 10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n", x);
}
int main() {
int a=10;
printf("a = %d before function call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function call_by_value.\n", a);
return 0;
}
The output of this call by value code example will look like this:
a = 10 before function call_by_value.
Inside call_by_value x = 10 before adding 10.
Inside call_by_value x = 20 after adding 10.
a = 10 after function call_by_value.
call by reference example
If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:
#include <stdio.h>
void call_by_reference(int *y) {
printf("Inside call_by_reference y = %d before adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after adding 10.\n", *y);
}
int main() {
int b=10;
printf("b = %d before function call_by_reference.\n", b);
call_by_reference(&b);
printf("b = %d after function call_by_reference.\n", b);
return 0;
}
The output of this call by reference source code example will look like this:
b = 10 before function call_by_reference.
Inside call_by_reference y = 10 before adding 10.
Inside call_by_reference y = 20 after adding 10.
b = 20 after function call_by_reference.
when to use which
One advantage of the call by reference method is that it is using pointers, so there is no doubling of the memory used by the variables (as with the copy of the call by value method). This is of course great, lowering the memory footprint is always a good thing. So why don’t we just make all the parameters call by reference?
There are two reasons why this is not a good idea and that you (the programmer) need to choose between call by value and call by reference. The reason are: side effects and privacy. Unwanted side effects are usually caused by inadvertently changes that are made to a call by reference parameter. Also in most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default and only use call by reference if data changes are expected.
call by name
In call-by-name evaluation, the arguments to a function are not evaluated before the function is called — rather, they are substituted directly into the function body (using capture-avoiding substitution) and then left to be evaluated whenever they appear in the function.
call by need
Lazy evaluation, or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations
I have created a guessing game that generates a random number, takes the user's guess, and basically outputs whether the user's guess is too high or too low(outputs blue, colder) or closer (warmer, outputs red) using both a text box background color and label, as you can see in my code. The issue I am having difficulties with is that every time I click submit, the program generates a new random number. I would like for the program to use the same number until the user's guess is correct, then it can generate a new number if the user would like to play again.
I'm thinking about using a while loop, such as
How could I possible make the random number stay static (same) until it's guessed correctly and if I do need a while loop, where would be the ideal place to place it?
Assign the generated random number to a variable, and then use that variable until you need a new random number.
In this particular instance, the line of code number = rand() % 1000 + 1; needs to be outside of your button click method. Otherwise, every time you click the button, it will generate a new random number.
What you want to do is move your number variable's declaration to the top of the class, outside any functions, like so:
class NumberGuessingGame
{
public: int number = 0;
}
Then, in your MyForm_Load method, you can generate the random number, and set it's value to that variable, like:
private: System::Void MyForm_Load()
{
//Set the value of number here to a newly generated random integer
}
You should then be able to access the number variable inside your button1_Click function:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (input > this->number)
{
//code
}
//Rest of your ifs
}
I suppose this:
public static string abc()
{
return "abc";
}
Is better to call this function in this way:
string call = abc();
Console.writeline(call);
Than this?
console.writeline(abc());
is there any reason to prefer one to the other?
Both are valid. However, out of experience I have concluded that the first option is more suitable for readability and ease of maintenance. I can't count how many times I have changed from the "compact" style to the first one as a help for a debugging session.
For example, this style makes it easy to check the correctness intermediate of an intermediate result:
string call = abc();
assert(!call.empty()); // Just an example.
Console.writeline(call);
Also, it helps to make the code more robust later, adding a conditional check before the subsequent action that checks call's value, for example if the design does not guarantee that the condition of the previous assert holds but you still need to check it.
string call = abc();
if (!call.empty())
{
Console.writeline(call);
}
Note also that with this style you will be able to easily inspect the value of call in your debugger.
Given your exact example (one parameter, value not used elsewhere, no side effects), it's just a matter of style. However, it gets more interesting if there are multiple parameters and the methods have side effects. For example:
int counter;
int Inc() { counter += 1; return counter }
void Foo(int a, int b) { Console.WriteLine(a + " " + b); }
void Bar()
{
Foo(Inc(), Inc());
}
What would you expect Foo to print here? Depending on the language there might not even be a predictable result. In this situation, assigning the values to a variable first should cause the compiler (depending on language) to evaluate the calls in a predictable order.
Actually I don't see a difference if you don't have any error checking.
This would make a difference
string call = abc();
# if call is not empty
{
Console.writeline(call);
}
The above method could avoid empty string being written.
I am doing some coding with OpenCV and I am processing some image pixel. However, the process take so much time (The picture is very delayed) because I am processing each color R G B on a sequence base. I thought I can make it faster by doing multi-threading and based on my previous knowledge Threadpool is more effecient. I saw some examples on line but they all requiring the use of QRunnable and my implementation should be easier than that because I just want to pass the same function with different channels every time
any idea !!!
If you want to execute a function in a separate thread you can use the QtConcurrentRun mechanism.
Suppose you have a function f with an integer as argument, in a class A
class A {
public:
f(int i);
}
Now if you want to call the function asynchronously, from a different class you can do:
A a;
QFuture<void> future1 = QtConcurrent::run(a, &A::f, 1); // Call it with argument 1
QFuture<void> future2 = QtConcurrent::run(a, &A::f, 2); // Call it with argument 2
You can use QFutureWatcher in order to get notified when the execution has finished.