ある種のコールバックメソッドを必要とするクラスを使用しているので、boost::functionを使用して関数ポインターを格納しています。
コールバックに1つのオプションの引数が必要ですが、boost :: functionではオプションの引数の種類を定義できないことがわかったので、次のコードを試してみましたが、うまくいきました。
//the second argument is optional
typedef boost::function< int (int, char*)> myHandler;
class A
{
public:
//handler with 2 arguments
int foo(int x,char* a) {printf("%s\n",a); return 0;};
//handler with 1 argument
int boo(int x) {return 1;};
}
A* a = new A;
myHandler fooHandler= boost::bind(&A::foo,a,_1,_2);
myHandler booHandler= boost::bind(&A::boo,a,_1);
char* anyCharPtr = "just for demo";
//This works as expected calling a->foo(5,anyCharPtr)
fooHandler(5,anyCharPtr);
//Surprise, this also works as expected, calling a->boo(5) and ignores anyCharPtr
booHandler(5,anyCharPtr);
私はそれが機能したことにショックを受けました、質問はそれが機能するべきか、そしてそれは合法ですか?
より良い解決策はありますか?