Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 94
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Here's the thing about calling a function inside a class: how private: public:, how to pass data to the class and how to get a result from the class. So far this is it!
Within the class, the result has to be calculated somehow. And how will the class without your involvement understand that it needs to return the result now, but not at some other time?
So, you need a method inside the class that can be called from outside. So it must be public.
Although of course you can make a private method that will read all the data inside the class and write it to the class variable, and the public method will call the private method. But some method must be called anyway. And what will be counted there in private method, how complex calculations will be, and what variables will be involved in calculations - everything is up to you, and on your own responsibility.
That's the point, how to call a function within a class, namely how private: public:, how to pass data to a class and how to get the result from the class. So far, here it is!
#property strict
#property script_show_inputs
//--- input parameters
input int inputA=1;
input int inputB=3;
class ASD
{
private:
// Здесь располагаются переменные и функции, доступные только внутри класса
public:
int Add(int a,int b)
{
Print(__FUNCTION__,": Результат сложения: a",a," + b",b," = ",a+b);
return(a+b);
}
} ar;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
ar.Add( inputA, inputB );
}
//+------------------------------------------------------------------+
I think it should work, I haven't checked it to see if it's correct)
Here you go, thank you, I'm already starting to move -
the script:
ASD ar;
void OnStart()
{
ar.fg (3,5);
ar.sf ();
}
class:
{
private:
int d;
int g (int a_s,int a_d){d=a_s+a_d;return (d=a_s+a_d); }
public:
void fg (int a_s,int a_d) { g (a_s,a_d); }
void sf () {Alert (d);}
};
Alert gives a result of 8, but it's an alert from the class, and I now need an alert from the script
Here, thank you, I'm already starting to move -
the script:
ASD ar;
void OnStart()
{
ar.fg (3,5);
ar.sf ();
}
class:
{
private:
int d;
int g (int a_s,int a_d){d=a_s+a_d;return (d=a_s+a_d); }
public:
void fg (int a_s,int a_d) { g (a_s,a_d); }
void sf () {Alert (d);}
};
Alert gives a result of 8, but it's an alert from the class, and now I need an alert from the script
Ok, you declare Alert() method in class and call it ar.Alert(), although now sf() method does the same. I'm not quite sure what you want to see after you've written a value to field d?
Looks like it should work, haven't checked it yet)
Everyone! Thank you so much! Got it -
script:
ASD ar;
void OnStart()
{
ar.fg (3,5);
Alert ( ar.sf ());
}
class:
{
private:
int d;
int g (int a_s,int a_d) {return (d=a_s+a_d); }
public:
void fg (int a_s,int a_d) { g (a_s,a_d); }
int sf () {return (d);}
};
I'll chew on it!
I don't remember who wrote me in this thread that the tester is broken, but I can answer that it may well be broken. It either swallows different chunks of time or starts counting from an arbitrary segment (although all the quotes are there).
I think who needs to see will understand.
?
(returns a number rounded up to a multiple of a given value)
or how to round, for example, 2285.13 to be a multiple of (0.25) 2285.00 __ 2285.25 ___ 2285.50 ___ 2285.75
how do I know the leverage of a pair rather than an account?
The amount of leverage provided: