templates and arrays using OOP

 

Hi everybody. Although this is little silly but I started learning OOP and unfortunately is started to like too much. Now I decided my just written fully working code rewrite. All started well, but after a while i got  stuck on one thing. I don´t know how to update arrays elements inside a class using templates.  I would initially want to include normal dynamic arrays and I would like the arrays to be private members of the class. Firstly I´ll try to avoid to CArrayObj ready made class. Here is my function code: 

int      lookback = RANGE;
int      ts_count = 0;
datetime t[];
double   c[];
template <typename TS>
void fill_ts_arr(TS &arr[],int bars, int &count, const ENUM_TIMEFRAMES cycle) 
{
    if (ArraySize(c)<bars)
    {
        ArrayResize(c,bars);
        ArrayResize(t,bars);
        for (int i = bars-1; i >= 0; i--) {c[i] = iClose(NULL,cycle,bars -i);}
        for (int i = bars-1; i >= 0; i--) {t[i] = iTime (NULL,cycle,bars -i);}
        count = bars;
    }
    else        
    {
        ArrayResize(c, count);
        ArrayResize(t, count);
        c[count-1] = iClose(NULL, cycle, 1);
        t[count-1] = iTime (NULL, cycle, 1);
    }
}

and there is call

C_process_M1.F_Process_Iteration(execution_cycle_M1);
if (C_process_M1.Get_Iterate_now()) {
    fill_ts_arr(c,lookback,ts_count,execution_cycle_M1);
    fill_ts_arr(t,lookback,ts_count,execution_cycle_M1);
    ts_count++; 
    }

I´ve read a lot of documentation , but I´m not biting through this big nut to convert it OOP. maybe someone has some good ideas to give or a little advice. 

Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

"It is forbidden to inherit classes from structures or structures from classes. Unlike C++,  MQL5 does not support multiple inheritance. A class can have at most one parent. A derived class object has a base class object built into it."

this is true statement or not ? maybe we can somehow bypass it or still not? We can´t access even ClassA "public" data from ClassB if ClassB is not a child of ClassA ? To use class data from another class, it is nessessary to copy *data in a separate function outside the class in order to transfer it to another class? I mean using only mql5. I know that we can build a .dll and then we can build our real pointer , but I would leave such extremes aside for the moment.

the more I research , the more I realize that it was totally stupid idea at all to put these arrays into the class private member. I am on the right track when I think that there is no point in encapsulating such frequently used data as [close] and [time]? It would be correct to leave them in the global space? Or put the basic class and build the whole system on top on this? How do You pro masters build your bigger systems and use global variables is a very big red light? 

 
titaanlabidas #:

"It is forbidden to inherit classes from structures or structures from classes. Unlike C++,  MQL5 does not support multiple inheritance. A class can have at most one parent. A derived class object has a base class object built into it."

this is true statement or not ? maybe we can somehow bypass it or still not? We can´t access even ClassA "public" data from ClassB if ClassB is not a child of ClassA ? To use class data from another class, it is nessessary to copy *data in a separate function outside the class in order to transfer it to another class? I mean using only mql5. I know that we can build a .dll and then we can build our real pointer , but I would leave such extremes aside for the moment.

the more I research , the more I realize that it was totally stupid idea at all to put these arrays into the class private member. I am on the right track when I think that there is no point in encapsulating such frequently used data as [close] and [time]? It would be correct to leave them in the global space? Or put the basic class and build the whole system on top on this? How do You pro masters build your bigger systems and use global variables is a very big red light? 

This is how I go about it in MQL5:

Generally I divide my code into 2 aspects.
One aspect is the code logic, which I generally code in procedural manner.

Let's say, I have a function to detect higher highs, lower lows, it will produce some type of data representing these higher highs and lower lows.

To store these data I use objects of structs or classes to group these data.

To store and manage these objects, I use things like fifo, filo buffers, containers, trees and alike. These are also implemented as objects, structs or classes.

These "containers" live in global space, to clean that up I use (usually) 2 name spaces, one for all my custom data, and another for holding general data. General data referes to OHLCV and alike.

This helps me in structuring my code into units, which I can hold in different files.

For me this works quite well, managing a codebase of around 12 MB in size. I don't loose oversight and I can find my functions easily in this file structure.

Also, limiting OOP to data has the advantage to bypass the limitation of MQLs inheritance model. Because logic doesn't play well with OOP model of MQL, it makes it very difficult to keep track of your code logic, if you implement it in "Java" style.


EDIT:
You might want to read about "is an" and "has an" relationship in OOP.

As MQL only supports one line of inheritance, you need to find a way to "squeeze" another object into the inheritance hierarchy, which can be done with templates, but it is limited in flexibility, but can be done to some extend.