如何重写CObject中的Compare(),使CList sort()工作? - 页 2

 
whroeder1:
  1. MT4/5没有final 关键字。

另外,这也是错误的--在MQL5中有一个'final'关键字。

class CFoo final
  {
  //--- class body
  };

class CBar : public CFoo
  {
  //--- class body
  };

这将不会让CBar编译,因为CFoo是最终的。

 
Amir Yacoby:
你错了,whroeder1。
不在基类中 添加virtual将导致你失去多态性--该方法将被静态调用,而不是在运行时动态调用。

这就是whroeder1提到的注意事项。

whroeder1:
  1. 不添加虚拟 是不好的做法,但不是必须的(除了在CObject中)。
  2. 不添加virtual 不会改变什么,它仍然可以在派生类中被重写。
  3. MT4/5没有final 关键字。
 
Amir Yacoby:

另外,这也是错误的--在MQL5中有一个'final'关键字。

在MQL4中也有一个final关键字,尽管文档中没有显示(据我所知)。可能是在构建更新中提到了它。
 
不清楚除了在CObject 中是什么意思,但无论如何,第1-2点是误导性的:
1.如果需要多态性,这不仅是不好的做法,而且在每个基地都需要(不仅是在CObject中)。
2.它确实改变了事情--见1。
3. final确实存在。
 
honest_knave:
在MQL4中也有一个最后的关键字,尽管文档中没有显示(据我所知)。可能是在构建更新中提到了它。
正确的!
 
Amir Yacoby:
正确!
它是MQL5的Build 1430。对于使用通用编译器的MQL4来说,可能是在同一时间。
 
honest_knave:
它是MQL5的Build 1430。可能在同一时间,MQL4的通用编译器也是如此。
是的,这就是我得到它的地方。文档滞后很多,检查 更改列表是很好的做法。
 

大家好。

我在一个和这个帖子一模一样的类中工作,但我的 "score "变量是一个方法。如何获得它的值?

这是该帖子开头的代码。

#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,const int mode=0);
   void           Price(const int p){price = p;}
   int            Price() const {return price;}
   void           Score(const int s){score = s;}
   int            Score() const {return score;}
  
};

int PriceScore::Compare(const CObject *node,const int 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)return 1;
   else if(pc.Score()> score) return -1;
   else return 0;
}


这就是我想做的事情。

#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){}
   virtual int    Compare(const CObject *node,const int mode=0) override const;
   void           setPrice(const int p){price = p;}
   //this is a simple function to get the close price of today
   double         getPrice()
                  {
                     double arrayPrice[1];
                     CopyClose(mySymbol, myTimeFrame, today, 1, arrayPrice);
                     return arrayPrice[0];
                  }
   void           Score(const int s){score = s;}
   int            Score() const {return score;}
  
};

int PriceScore::Compare(const CObject *node,const int 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()???
      return 1;
   else if(pc.Score() > getPrice()) //here is the problem, how to use getPrice()???
      return -1;
   else
      return 0;
}

问题是,如何在方法Compare()中使用getPrice()?

谢谢
 
Gustavo Hennemann:

大家好。

我在一个和这个帖子一模一样的类中工作,但我的 "score "变量是一个方法。如何获得它的值?

这是该帖子开头的代码。


这就是我想做的事情。

问题是,如何在方法Compare()中使用getPrice()?

谢谢

用 const 关键字声明你的 getPrice() 方法。

   double         getPrice() const
 
Alain Verleyen:

用const关键字声明你的getPrice()方法。

你好@Alain Verleyen,

我改变了getPrice()的方法,用CopyClose()代替了CopyBuffer()。这并没有改变主要目标。

因此,如果我在getPrice()方法中使用 "const "关键字,就会出现错误。"'CopyBuffer'--没有一个重载可以应用于该函数调用"。我认为这是因为CopyBuffer不是一个const方法,而且不可能在const方法中调用非const方法。