我在C++中使用函數數組的經驗並不多。我需要使用一個函數數組,其中數組包含來自不同對象的函數。 下面是一些虛擬代碼來說明我想要實現的內容。具有不同對象功能的函數陣列
class base_class
{
public:
virtual int function1(int arg1, int arg2);
virtual int function2(int arg1, int arg2);
};
class derived_class : public base_class
{
public:
int function1(int arg1, int arg2) { /* ... */ };
int function2(int arg1, int arg2) { /* ... */ };
// ...
};
typedef int (*functions) (int arg1, int arg2);
int main()
{
derived_class object1;
derived_class object2;
functions func_instance[4];
func_instance[0] = object1.function1;
func_instance[1] = object1.function2;
func_instance[2] = object2.function1;
func_instance[3] = object2.function2;
// ...
}
我無法得到它的工作,它拋出以下錯誤:
error: argument of type int() (int , int) does not match int (*) (int, int)
查一查'的std :: function'和'的std :: bind'。 – Antimony 2013-05-14 07:16:24
歡迎來到SO。請查看http://stackoverflow.com/faq瞭解網站的工作原理。請將您的問題的最佳答案標記爲已接受並相應地進行投票。玩得開心:) – pmr 2013-05-14 21:52:39