MQL4 结构包含对其他结构的地址



我需要编写类似的东西,但我不知道第二个结构中的正确语法来包含第一个类型结构的地址的字段。

struct ConditionSet
{
int            CondsNbr;                     // Number of cond-s in the set
bool           TabConds         [MaxConditions];
string         TabCondsLabel    [MaxConditions];
int            CandleNum        [MaxConditions];
bool           ExitCondition;
int            int1, int2, int3, int4, int5; // user integers
double         d1, d2, d3,d4, d5;            // user doubles
};
struct Transaction
{
string         Strategie_name;
string         Symbol;
bool           BuyReady;
bool           SellReady;
bool           BuyRunning;
bool           SellRunning;
ConditionSet  & conditionsAchat;   // HERE, THIS IS NOT A CORRECT SYNTAX
ConditionSet  & conditionsVente;   // HERE, THIS IS NOT A CORRECT SYNTAX
int            ticketAchat;
int            ticketVente;
};

如果结构包含字符串类型和/或动态数组对象的变量,编译器会将隐式构造函数分配给此类结构。此构造函数重置字符串类型的所有结构成员,并正确初始化动态数组的对象。对象

指针

在 MQL4 中,可以动态创建复杂类型的对象。这是由 new 运算符完成的,该运算符返回所创建对象的描述符。描述符大小为 8 个字节。在语法上,MQL4 中的对象描述符类似于C++ 中的指针。

MyObject* hobject= new MyObject();

与C++相反,上面示例中的hobject变量不是指向内存的指针,而是对象描述符。此外,在 MQL5 中,函数参数中的所有对象都必须通过引用传递。

//+------------------------------------------------------------------+
//| Objects are always passed by reference                           |
//+------------------------------------------------------------------+
void PrintObject(Foo &object)
{
Print(__FUNCTION__,": ",object.m_id," Object name=",object.m_name);
}
//+------------------------------------------------------------------+
//| Passing an array of objects                                      |
//+------------------------------------------------------------------+
void PrintObjectsArray(Foo &objects[])
{
int size=ArraySize(objects);
for(int i=0;i<size;i++)
{
PrintObject(objects[i]);
}
}
//+------------------------------------------------------------------+
//| Passing an array of pointers to object                           |
//+------------------------------------------------------------------+
void PrintPointersArray(Foo* &objects[])
{
int size=ArraySize(objects);
for(int i=0;i<size;i++)
{
PrintObject(objects[i]);
}
}
//+------------------------------------------------------------------+
class Foo
{
public:
string            m_name;
int               m_id;
static int        s_counter;
//--- constructors and desctructors
Foo(void){Setup("noname");};
Foo(string name){Setup(name);};
~Foo(void){};
//--- initializes object of type Foo
void              Setup(string name)
{
m_name=name;
s_counter++;
m_id=s_counter;
}
};
//+------------------------------------------------------------------+
int Foo::s_counter=0;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//--- declare an object as variable with its automatic creation
Foo foo1;
//--- variant of passing an object by reference
PrintObject(foo1);
//--- declare a pointer to an object and create it using the 'new' operator
Foo *foo2=new Foo("foo2");
//--- variant of passing a pointer to an object by reference
PrintObject(foo2); // pointer to an object is converted automatically by compiler
//--- declare an array of objects of type Foo
Foo foo_objects[5];
//--- variant of passing an array of objects
PrintObjectsArray(foo_objects); // separate function for passing an array of objects
//--- declare an array of pointers to objects of type Foo
Foo *foo_pointers[5];
for(int i=0;i<5;i++)
{
foo_pointers[i]=new Foo("foo_pointer");
}
//--- variant of passing an array of pointers
PrintPointersArray(foo_pointers); // separate function for passing an array of pointers
//--- it is obligatory to delete objects created as pointers before termination
delete(foo2);
//--- delete array of pointers
int size=ArraySize(foo_pointers);
for(int i=0;i<5;i++)
delete(foo_pointers[i]);
//---   
}
//+------------------------------------------------------------------+

关键字this

类类型(对象(的变量可以通过引用和指针传递。除了引用之外,指针还允许访问对象。声明对象指针后,应向其应用 new 运算符以创建和初始化它。

保留字this用于获取对象对自身的引用,该引用在类或结构方法中可用。this始终引用对象,在使用它的方法中,表达式GetPointer(this)给出对象的指针,其成员是执行GetPointer()调用的函数。在 MQL4 中,函数不能返回对象,但可以返回对象指针。

感谢您的回复。

将 Foo 类移动到源代码的顶部并添加 strict 属性后,我可以编译您的源代码。

我不明白这句话 int Foo::s_counter=0;

它做什么?

我在想课程如何解决我的问题,但这并不容易。

詹菲

最新更新