我有一个结构体,在其中有指向该结构体的相同函数的指针。现在我需要调用该结构体之外的函数指针。以下是代码示例:
#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)();
- 为什么你把它放在类外面?移除它并修正你的调用语法。 - Mat