如何将一个方法指针用作另一个方法的参数
How do use a method pointer as a parameter of another method?
我到处都在寻找解决方案,可能只是我不知道如何正确地用语言表达。
我希望这个firstFunction
在firstFunction
的参数中声明其地址时调用otherFunction
。
这是我的代码:
init.h:
class Ainit
{
//function to be passed into firstFunction
void test();
void firstFunction( void (Ainit::*otherFunction)() );
};
init.cpp:
void Ainit::firstFunction( void (Ainit::*otherFunction)() )
{
(Ainit::*otherFunction)();
}
Xcode:指向以下行中的*
,错误为:Expected unqualified-id
(Ainit::*otherFunction)();
如何在firstFunction
中调用传递给firstFunction
的方法?
将代码更改为:
(this->*otherFunction)();
在声明firstFunction的参数和调用*otherFunction时,不需要Ainit::。以下代码做得很好:
class Ainit {
void test();
void firstFunction (void * otherFunction());
};
void Ainit::test() {
std::cout << "Hello" << std::endl;
}
void Ainit::firstFunction(void * otherFunction()) {
(*otherFunction)();
}
相关文章:
- 将一个类的方法指针存储到另一个类中
- 如何调用返回类方法指针的类方法
- 通过reinterpret_casting方法指针从指针调用派生类的方法。这是 UB 吗?
- 泛型方法指针.reinterpret_cast指向不同类的方法指针,这是 UB 吗?
- C++;类方法指针;λ;将 lambda 作为成员函数指针传递;
- 使用模板、方法指针和字符串键入推导
- 将方法指针作为整数参数发送到C#的C 方法
- 从C 中的VTable获取方法指针
- 模板化方法指针 - 无法匹配函数参数的指针
- 获取特定的模板重载方法指针
- 如何将方法指针声明为Typedef方法参数
- 如何将方法指针类型转换为函数指针类型
- 如何在使用 pthreads 时将方法指针作为函数参数传递C++
- 方法指针映射,编译器说他们不接受任何参数
- C# 方法指针,如 C++ 中的指针
- c++创建新的LinkedList类:Clear()方法指针被释放未分配
- 尝试获取类方法指针时出现E_NOINTERFACE
- 传递和强制转换方法指针
- 常量方法指针的类型是什么
- 将对象的方法指针传递给接受 [静态方法指针/全局函数] 指针的函数