将指定的函数分配给函数成员指针?

时间:2015-03-11 00:08:10

标签: c++ struct function-pointers member-pointers

鉴于

void* hello () {
   cout << "Test.\n";          
}

struct _table_struct {
    void *(*hello) ();
};

我们如何将函数(hello)赋值给函数成员指针?

我试过这个(主要的):

 _table_struct g_table;
 _table_struct *g_ptr_table = &g_table;

 // trying to get the struct member function pointer to point to the designated function
 (*(g_ptr_table)->hello) =  &hello; // this line does not work

 // trying to activate it
 (*(g_ptr_table)->hello)();

1 个答案:

答案 0 :(得分:1)

在将指针指向对象时,不要取消引用指针。

g_ptr_table->hello = &hello; // note: the & is optional
g_ptr_table->hello(); // dereferencing the function pointer is also optional
相关问题