模板和指向成员函数的指针
templates and pointers to member functions
我想使用指向某个成员函数的指针并与之一起工作,我还希望能够回调该(或其他)成员函数。比方说,我有这样的标题:
class A{
public:
template<class T> void Add(bool (T::*func)(), T* member){
((member)->*(func))();
}
};
class B{
public:
bool Bfoo(){return 1;}
void Bfoo2(){
A a;
a.Add(Bfoo, this);
}
};
和类似的cpp:
main(){
B f;
f.Bfoo2();
}
我有以下错误:
main.h(22):错误C2784:"void __thiscall A::Add(bool(__thiscaallT: :*)(void),T*)":无法推导"重载的模板参数"重载函数类型"中的"函数类型"
我需要从许多类中调用A::Add(并发送关于类方法及其实例的信息),所以这就是我想要使用模板的原因
使用Microsoft Visual C++6.0。我做错了什么?我不能使用助推。
在我看来,正确的方法是使用继承,例如:
class A {
virtual void Add() = 0;
}
class B : public A {
void Add() {...}
}
class C : public A {
void Add() {...}
}
所以在你的主要任务中,你可以做:
A* a = new B();
a->Add(); /* this call B::Add() method */
您需要传递函数的地址
a.Add(&B::Bfoo, this);
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针