question class creation error

 

Hello evrybody,

I have a problem setting a symbol name to a strategy i created. this is some test code to reproduce the problem...

//+------------------------------------------------------------------+
//|                                                         test.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2010
//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
// #import "user32.dll"
//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
//   string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
#define EXPERT_NAME "test"   // Name of the Expert Advisor
class Strategy {
                     public:
                     string symbol, name;
                     Strategy(){ symbol = ""; name = "";}
                     ~Strategy(){}
                     };
         class Instrument {
                     public:
                           Strategy *sAr[];
                           string symbol;
                           Instrument(){
                                 int size = ArrayResize(sAr, 3);
                                 for(int s=0;s<size;s++){ sAr[s] = stratAr[s];}
                                 }
                           ~Instrument(){}
                           void Init();
                        };
           void Instrument::Init(){ 
                                 int size = ArraySize(sAr);
                                 for(int i=0;i<size;i++){sAr[i].symbol = symbol;}
                                 }                       
           static Instrument *insAr[]; 
           static Strategy *stratAr[];
           
       int OnInit(){
                     
                     //for(;s-- > 0;){ start = new RedTest().id = s;Print(start.R_name);} // creating 10 red objects and add 1 child blue.
                     ArrayResize(stratAr,3);
                              stratAr[0] = new Strategy();
                              stratAr[0].name = "first strategy";
                              stratAr[0].symbol = "";
                              stratAr[1] = new Strategy();
                              stratAr[1].name = "second strategy";
                              stratAr[1].symbol = "";
                              stratAr[2] = new Strategy();
                              stratAr[2].name = "third strategy";
                              stratAr[2].symbol = "";
                     
                     ArrayResize(insAr,6);
                     for(int v=0; v<6;v++){
                                 insAr[v] = new Instrument();
                                 insAr[v].symbol = "symbol_"+v;
                                 insAr[v].Init();
                                 } 
                     Print("1 - "+insAr[0].symbol+" ==> "+insAr[0].sAr[0].name+" ==> "+insAr[0].sAr[0].symbol);
                     Print("2 - "+insAr[0].symbol+" ==> "+insAr[0].sAr[1].name+" ==> "+insAr[0].sAr[1].symbol);
                     Print("3 - "+insAr[0].symbol+" ==> "+insAr[0].sAr[2].name+" ==> "+insAr[0].sAr[2].symbol);
                     Print("4 - "+insAr[1].symbol+" ==> "+insAr[1].sAr[0].name+" ==> "+insAr[1].sAr[0].symbol);
                     Print("5 - "+insAr[1].symbol+" ==> "+insAr[1].sAr[1].name+" ==> "+insAr[1].sAr[1].symbol);
                     Print("6 - "+insAr[1].symbol+" ==> "+insAr[1].sAr[2].name+" ==> "+insAr[1].sAr[2].symbol);
                     Print("7 - "+insAr[2].symbol+" ==> "+insAr[2].sAr[0].name+" ==> "+insAr[2].sAr[0].symbol);
                     Print("8 - "+insAr[2].symbol+" ==> "+insAr[2].sAr[1].name+" ==> "+insAr[2].sAr[1].symbol);
                     Print("9 - "+insAr[2].symbol+" ==> "+insAr[2].sAr[2].name+" ==> "+insAr[2].sAr[2].symbol);
                     
                     
            return(INIT_SUCCEEDED);
               } 
    
    //-----------------------------------------------------+
            void OnDeinit(const int reason){
                              //--- When deleting from the chart
                              if(reason == REASON_REMOVE){
                                             //--- Delete all objects relating to the info panel from the chart
                                       int s = ArraySize(insAr);      
                                       for(;s-- > 0;){ delete insAr[s];};
                                       int r = ArraySize(stratAr);      
                                       for(;r-- > 0;){ delete stratAr[r];};
                                     
                                   }
                 }
   //+------------------------------

 The problem is after setting the symbol to the strategy on the Init() function the symbol name of the last symbol is copied to all insAr[x].sAr[y].symbol

i don't understand why because insAr[x] is an object. the variable symbol of insAr[x] should hold the symbol of that instrument but it looks like its being copied over by the next insAr[x+1] ???

thank you 

result: 

2014.09.21 13:48:23.421 test (GBPUSD,H1) 9 - symbol_2 ==> third strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 8 - symbol_2 ==> second strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 7 - symbol_2 ==> first strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 6 - symbol_1 ==> third strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 5 - symbol_1 ==> second strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 4 - symbol_1 ==> first strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 3 - symbol_0 ==> third strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 2 - symbol_0 ==> second strategy ==> symbol_5

2014.09.21 13:48:23.421 test (GBPUSD,H1) 1 - symbol_0 ==> first strategy ==> symbol_5

 
AMvanRijsbergen:

Hello evrybody,

I have a problem setting a symbol name to a strategy i created. this is some test code to reproduce the problem...


What a mess. Why are you using OOP ? It seems you don't understand what you are doing.

Just one example :

     //--- Constructor 
     Instrument()
     {
      int size = ArrayResize(sAr, 3);
      for(int s=0;s<size;s++){ sAr[s]=stratAr[s];}
     }

stratAr[] is a global array, that breaks encapsulation.

public:
   Strategy         *sAr[];
   string            symbol;

Your Strategy class contains a property "symbol" and you add again a property "symbol" to your Instrument class, why ?

 The problem is after setting the symbol to the strategy on the Init() function the symbol name of the last symbol is copied to all insAr[x].sAr[y].symbol

i don't understand why because insAr[x] is an object. the variable symbol of insAr[x] should hold the symbol of that instrument but it looks like its being copied over by the next insAr[x+1] ???

You set the same Strategy array to you Instrument in your constructor :

      for(int s=0;s<size;s++){ sAr[s]=stratAr[s];}  // Instrument constructor

Then you set the same value to symbol for the 3 strategies.

   for(int i=0;i<size;i++){sAr[i].symbol=symbol;}   // Instrument Init

As your Strategy array is global you change the same value each time, and only the last one is kept.

I still wonder what you are trying to do ?