#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;
}
そのため、getPrice()メソッドで "const "キーワードを使用すると、エラーが発生します。"'CopyBuffer' - no one of the overloads can be applied to the function call"というエラーが発生します。これは、CopyBufferがconstメソッドではなく、constメソッド内で非constメソッドを呼び出すことができないために発生すると思われます。
また、これは間違いです。MQL5には'final'というキーワードがあります。
{
//--- class body
};
class CBar : public CFoo
{
//--- class body
};
この場合、CFooがfinalであるため、CBarはコンパイルできません。
whroeder1さん、あなたはここで間違っています。
メソッドは静的に呼び出され、実行時に動的に呼び出されることはないのです。
これは、whroeder1が言っていた注意点です。
また、これは間違いです。MQL5には「final」キーワードがあります。
1. ポリモーフィズムが必要な場合は、バッドプラクティスどころか、すべてのベース(CObjectに限らず)で要求されます。
2.
3. final は存在する。
ドキュメントには(私の知る限り)記載されていませんが、MQL4にもfinalキーワードがあります。もしかしたら、ビルドアップデートで言及されたのかもしれません。
正解!
MQL5のBuild 1430でした。おそらく、共通コンパイラのMQL4も同じ頃だと思います。
こんにちは、皆さん。
私はこの記事のようなクラスで作業していますが、私の "スコア "変数は、メソッドです。どのようにその値を取得するには?
これは、投稿の冒頭からのコードです。
これは私がやろうとしていることです。
質問は、メソッドCompare()の中でgetPrice()をどのように使用するかです。
ありがとうございますこんにちは、皆さん。
私はこの記事のようなクラスで作業していますが、私の "スコア "変数は、メソッドです。どのようにその値を取得するには?
これは、投稿の冒頭からのコードです。
これは私がやろうとしていることです。
問題は、メソッドCompare()の中でgetPrice()をどのように使用するかです。
ありがとうございます。constキーワードでgetPrice()メソッドを宣言してください。
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メソッドを呼び出すことができないために発生すると思われます。