コードの重複を避けるために、静的メソッドの引数として関数へのポインターを渡すようにしています。
静的メソッドのみを持つクラス (Geo) があります。このメソッドの 1 つ (+++Geo::traceRay(+++)) は、いくつかのものを表示 (Geo::display(+++)) してから、int を返す必要があります。
別のクラス (Las) は Geo::traceRay(+++) メソッドを使用する必要がありますが、別の方法で表示 (Las::display(+++)) する必要があります。そこで、Geo::traceRay(+++, 関数へのポインター) メソッドに関数へのポインター引数を渡そうとします。指摘された関数は、正しい「display()」メソッドになります。
これまでのところ、最初のポインターを display() に渡すことは問題ではありませんが、2 番目のポインターでそれを行う方法が見つかりません。
class Geo
{
public:
static bool display(int pix);
static int traceRay(int start, int end, bool (*func)(int) = &Geo::display); // no issue with this default parameter
};
class Las
{
public:
bool display(int pix);
void run();
};
int Geo::traceRay(int start, int end, bool (*func)(int))
{
for (int i = start; i < end ; ++i )
{
if((*func)(i)) return i;
}
return end;
}
bool Geo::display(int pix)
{
cout << pix*100 << endl;
return false;
}
bool Las::display(int pix)
{
cout << pix << endl;
if (pix == 6) return true;
return false;
}
void Las::run()
{
bool (Las::*myPointerToFunc)(int) = &display; // I can just use display as a non member class, but it should stay a member
Geo::traceRay(0,10, myPointerToFunc); // issue here!
}
int main()
{
Geo::traceRay(0,10); // use the "normal display" = the default one// OK
Las myLas;
myLas.run();
return 0;
}