CObjectでCompare()をオーバーライドして、CList sort()を動作させるには? - ページ 2

 
whroeder1:
  1. MT4/5にはfinal キーワードはありません。

また、これは間違いです。MQL5には'final'というキーワードがあります。

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

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

この場合、CFooがfinalであるため、CBarはコンパイルできません。

 
Amir Yacoby:
whroeder1さん、あなたはここで間違っています。
メソッドは静的に呼び出され、実行時に動的に呼び出されることはないのです。

これは、whroeder1が言っていた注意点です。

whroeder1:
  1. virtualを 付けないのは悪い習慣ですが、必須ではありません(CObject.を除く)。
  2. virtualを付けなくても 何も変わらないし、派生クラスでオーバーライドできる。
  3. MT4/5にはfinal キーワードはありません。
 
Amir Yacoby:

また、これは間違いです。MQL5には「final」キーワードがあります。

MQL4にもfinalキーワードはありますが、ドキュメントには(私の知る限り)記載されていません。もしかしたら、ビルドアップデートで言及されたのかもしれません。
 
CObject 以外が何を意味するのかは不明ですが、いずれにせよ、1-2点は誤解を招きます。
1. ポリモーフィズムが必要な場合は、バッドプラクティスどころか、すべてのベース(CObjectに限らず)で要求されます。
2.
3. final は存在する。
 
honest_knave:
ドキュメントには(私の知る限り)記載されていませんが、MQL4にもfinalキーワードがあります。もしかしたら、ビルドアップデートで言及されたのかもしれません。
正しい!
 
Amir Yacoby:
正解!
MQL5のBuild 1430でした。おそらく、共通コンパイラのMQL4も同じ頃だと思います。
 
honest_knave:
MQL5のBuild 1430でした。おそらく、共通コンパイラのMQL4も同じ頃だと思います。
はい、そこから入手しました。ドキュメントは何度も遅れるので、変更リストをチェック するのは良い習慣です。
 

こんにちは、皆さん。

私はこの記事のようなクラスで作業していますが、私の "スコア "変数は、メソッドです。どのようにその値を取得するには?

これは、投稿の冒頭からのコードです。

#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:

こんにちは、皆さん。

私はこの記事のようなクラスで作業していますが、私の "スコア "変数は、メソッドです。どのようにその値を取得するには?

これは、投稿の冒頭からのコードです。


これは私がやろうとしていることです。

問題は、メソッドCompare()の中でgetPrice()をどのように使用するかです。

ありがとうございます。

constキーワードでgetPrice()メソッドを宣言してください。

   double         getPrice() const
 
Alain Verleyen:

getPrice()メソッドをconstキーワードで宣言してください。

こんにちは、@Alain Verleyen です。

getPrice()メソッドを変更しました。CopyClose()の代わりにCopyBuffer()を使っています。これは、主な目的を変更するものではありません。

そのため、getPrice()メソッドで "const "キーワードを使用すると、エラーが発生します。"'CopyBuffer' - no one of the overloads can be applied to the function call"というエラーが発生します。これは、CopyBufferがconstメソッドではなく、constメソッド内で非constメソッドを呼び出すことができないために発生すると思われます。