【发布时间】:2018-10-14 12:36:23
【问题描述】:
我有一个结构,里面有一个指向同一结构的函数的指针。现在我需要调用一个指向结构外函数的指针。我举一个下面的代码示例:
#include <iostream>
struct test {
void (test::*tp)(); // I need to call this pointer-to-function
void t() {
std::cout << "test\n";
}
void init() {
tp = &test::t;
}
void print() {
(this->*tp)();
}
};
void (test::*tp)();
int main() {
test t;
t.init();
t.print();
(t.*tp)(); // segfault, I need to call it
return 0;
}
【问题讨论】:
-
void (test::*tp)();- 你为什么在课外有这个。删除它并修复你的调用语法。
标签: c++ member-function-pointers