1

我有 3 个相同类型的单独结构数组,例如:

MqlTradeRequest orders1[];
MqlTradeRequest orders2[];
MqlTradeRequest orders3[];

与此处描述的 C类似。

假设稍后填充这些数组,我如何创建指向其中之一的公共指针?


所以我可以做类似的事情:orders_ptr = &orders1*orders_ptr = orders1

这是我的非工作代码:

MqlTradeRequest orders1[];
MqlTradeRequest orders2[];
MqlTradeRequest orders3[];

enum ORDERS_POOL {
  POOL1,
  POOL2,
  POOL3
};

void start(ORDERS_POOL _pool = POOL1) {
  MqlTradeRequest (*orders_ptr)[]; // Error: Invalid operation use.
  switch (_pool) {
    case POOL1: orders_ptr = &orders1; break; // Error: Invalid array access, class type expected.
    case POOL2: orders_ptr = &orders2; break; // Error: Invalid array access, class type expected.
    case POOL3: orders_ptr = &orders2; break; // Error: Invalid array access, class type expected.
  }
  for (int i = 0; i < ArraySize(orders_ptr); i++) {
    Print(orders_ptr[i].order);
  }
};

这是另一个尝试:

  MqlTradeRequest *orders_ptr; // Error: Invalid operation use.
  switch (_pool) {
    case POOL1: *orders_ptr = GetPointer(orders1); break; // Error: Object pointer expected.
    case POOL2: *orders_ptr = GetPointer(orders2); break; // Error: Object pointer expected.
    case POOL3: *orders_ptr = GetPointer(orders2); break; // Error: Object pointer expected.
  }

通过不同的池,我的意思是类似于在OrderSelect中使用它的pool参数完成的事情,但我的池完全不同。

但是上面的代码失败了,有很多没有任何意义的错误,我已经在评论中包含了其中的一些。

什么是正确的方法?

我的目标是分配一个指向结构数组的指针,这样我就可以遍历选定的数组。

4

3 回答 3

1

在我看来,您不能在 MQL5 中做到这一点。
您只能将指针用于动态对象,如类,对于结构,它是静态指针,与数组或原始指针相同,并且MqlRates是结构而不是类。
如果你真的需要这样的魔法 - 创建其中包含 Mql 结构数组的类。

于 2017-01-18T15:21:36.950 回答
0

As per MQL Support Team reply, it is not possible to create pointers to the struct type objects nor you cannot get the pointer to an array.

Instead it is suggested to wrap MqlTradeRequest array to the class object (CTradeReqArray for example), then it's possible to choose desired array and get the pointer to the CTradeReqArray object, as suggested in Daniel's answer.

于 2017-02-13T19:49:32.337 回答
0

您可以像这样使用指向同一类对象的指针数组(在使用本机 ArrayObj 时具有优势)-当然,如果您真的需要,可以在类元素中封装您的数组):

//+------------------------------------------------------------------+
//|                                               test_myArrPtrs.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Arrays\ArrayObj.mqh>

class elD  : public CObject
{  
public:    
   double m_name;
   int m_id;  
        
    elD(){};   //Constructor     
};

CArrayObj  arrD;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    elD *el=new elD();
    el.m_name = 1.4000;
    el.m_id = 100;

    arrD.CreateElement(0);
    arrD.Add(el);
    elD *e=arrD.At(0);

    Print(e.m_id);
   arrD.Delete(0);
   
   // only dynamic objects should be deleted
   // if you try to delete a static object then you will obviously get errors. The correct way to delete an object is as follows. 
   if (CheckPointer(el) == POINTER_DYNAMIC)
      delete el;

   if (CheckPointer(e) == POINTER_DYNAMIC)
      delete e;

    return;
  }
//+------------------------------------------------------------------+
于 2021-08-24T14:22:54.610 回答