#include <Arrays\List.mqh>
//+------------------------------------------------------------------+//| Script program start function |//+------------------------------------------------------------------+class PriceScore : public CObject
{
protected:
int price;
int score;
public:
PriceScore(void){}
PriceScore(int p, int s):price(p),score(s){}
~PriceScore(void){}
int Compare(const CObject *node,constint mode=0);
void Price(constint p){price = p;}
int Price() const {return price;}
void Score(constint s){score = s;}
int Score() const {return score;}
};
int PriceScore::Compare(const CObject *node,constint mode=0) //Can't call this override from CList
{
PriceScore *pc = (PriceScore*)node;
Print(__FUNCTION__,":Compare called. Incoming: ",pc.Score()," This: ", score); //Doesn't log because this isn't called from CObject'if(pc.Score()< score)return1;
elseif(pc.Score()> score) return -1;
elsereturn0;
}
这就是我想做的事情。
#include <Arrays\List.mqh>
//+------------------------------------------------------------------+//| Script program start function |//+------------------------------------------------------------------+class PriceScore : public CObject
{
protected:
int price;
int score;
public:
PriceScore(void){}
PriceScore(int p, int s):price(p),score(s){}
~PriceScore(void){}
virtualint Compare(const CObject *node,constint mode=0) override const;
void setPrice(constint p){price = p;}
//this is a simple function to get the close price of todaydouble getPrice()
{
double arrayPrice[1];
CopyClose(mySymbol, myTimeFrame, today, 1, arrayPrice);
return arrayPrice[0];
}
void Score(constint s){score = s;}
int Score() const {return score;}
};
int PriceScore::Compare(const CObject *node,constint mode=0) const
{
PriceScore *pc = (PriceScore*)node;
Print(__FUNCTION__,":Compare called. Incoming: ",pc.Score()," This: ", score);
if(pc.Score() < getPrice()) //here is the problem, how to use getPrice()???return1;
elseif(pc.Score() > getPrice()) //here is the problem, how to use getPrice()???return -1;
elsereturn0;
}
另外,这也是错误的--在MQL5中有一个'final'关键字。
{
//--- class body
};
class CBar : public CFoo
{
//--- class body
};
这将不会让CBar编译,因为CFoo是最终的。
你错了,whroeder1。
不在基类中 添加virtual将导致你失去多态性--该方法将被静态调用,而不是在运行时动态调用。
这就是whroeder1提到的注意事项。
另外,这也是错误的--在MQL5中有一个'final'关键字。
1.如果需要多态性,这不仅是不好的做法,而且在每个基地都需要(不仅是在CObject中)。
2.它确实改变了事情--见1。
3. final确实存在。
在MQL4中也有一个最后的关键字,尽管文档中没有显示(据我所知)。可能是在构建更新中提到了它。
正确!
它是MQL5的Build 1430。可能在同一时间,MQL4的通用编译器也是如此。
大家好。
我在一个和这个帖子一模一样的类中工作,但我的 "score "变量是一个方法。如何获得它的值?
这是该帖子开头的代码。
这就是我想做的事情。
问题是,如何在方法Compare()中使用getPrice()?
谢谢大家好。
我在一个和这个帖子一模一样的类中工作,但我的 "score "变量是一个方法。如何获得它的值?
这是该帖子开头的代码。
这就是我想做的事情。
问题是,如何在方法Compare()中使用getPrice()?
谢谢用 const 关键字声明你的 getPrice() 方法。
用const关键字声明你的getPrice()方法。
你好@Alain Verleyen,
我改变了getPrice()的方法,用CopyClose()代替了CopyBuffer()。这并没有改变主要目标。
因此,如果我在getPrice()方法中使用 "const "关键字,就会出现错误。"'CopyBuffer'--没有一个重载可以应用于该函数调用"。我认为这是因为CopyBuffer不是一个const方法,而且不可能在const方法中调用非const方法。