我需要编写类似的代码,但我不知道第二种结构中的正确语法是否具有包含第一种类型的地址的字段。
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;
};
答案 0 :(得分:0)
如果结构包含字符串类型的变量和/或动态数组的对象,则编译器会为该结构分配隐式构造函数。此构造函数将重置字符串类型的所有结构成员,并正确初始化动态数组的对象。
对象指针
在MQL4中,可以动态创建复杂类型的对象。这由new运算符完成,该运算符返回所创建对象的描述符。描述符的大小为8个字节。从语法上讲,MQL4中的对象描述符与C ++中的指针 相似。
MyObject* hobject= new MyObject();
在与C ++的 中,上例中的
hobject
变量为 not 一个指向内存的指针,而是一个对象描述符。此外,在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中,函数无法返回对象,但是它们可以返回对象指针。
答案 1 :(得分:0)
谢谢您的回复。
将Foo类移到源代码的顶部,并添加strict属性后,我可以编译您的源代码。
我不明白这条线 int Foo :: s_counter = 0;
它做什么?
我在考虑类如何解决我的问题,但这并不容易。
Janfi