绑定成员函数的地址
Address of bound member function
我有一个如下的成员函数:
void GClass::InitFunctions()
{ // Initialize arrays of pointers to functions.
ComputeIntLen[0] = &ComputeILS;
ComputeIntLen[1] = &ComputeILE;
ComputeIntLen[2] = &ComputeILI;
PostStep[0] = &PSS;
PostStep[1] = &PSE;
PostStep[2] = Ψ
gRotation = new Rotation();
}
GClass显然包含所有相关的成员-:
void ComputeILE(Int_t, Int_t *, Double_t *);
void ComputeILI(Int_t, Int_t *, Double_t *);
void PSS(Int_t , Int_t *, Int_t &, Int_t*);
void PSE(Int_t, Int_t *, Int_t &, Int_t*);
void PSI(Int_t , Int_t *, Int_t &, Int_t*);
ComputeIntLenFunc ComputeIntLen[gNproc];
PostStepFunc PostStep[gNproc];
... //other members
}
其中gNproc是一个全局const int, ComputeIntLenFunc和PostStepFunc是这样的类型定义:
typedef void (*ComputeIntLenFunc)(Int_t ntracks, Int_t *trackin, Double_t *lengths);
typedef void (*PostStepFunc)(Int_t ntracks, Int_t *trackin, Int_t &nout, Int_t* trackout);
当我编译这个时,我得到gcc给出了一个错误:"ISO c++禁止使用非限定的或带括号的非静态成员函数的地址来形成指向成员函数的指针。说' &GClass::ComputeIntLenScattering ' "
当我用GClass::FunctionNames替换InitFunctions()中的FunctionNames时,我得到"无法在赋值中将' void (GClass::*)(Int_t, Int_t*, Double_t*) '转换为' void (*)(Int_t, Int_t*, Double_t*) ' "
请帮帮我。
非静态成员函数指针不同于自由函数指针。你正在使用的基本上是自由函数指针类型,这是行不通的,因为&GClass::ComputeILS
的类型与ComputeIntLenFunc
不兼容。
:
typedef void (GClass::*ComputeIntLenFunc)(Int_t, Int_t *, Double_t *);
typedef void (GClass::*PostStepFunc)(Int_t, Int_t *, Int_t &, Int_t*);
我省略了参数名,因为它们在类型定义中不需要。
另外,当你得到成员函数的地址时,你必须使用GClass::
:
ComputeIntLen[0] = &GClass::ComputeILS;
ComputeIntLen[1] = &GClass::ComputeILE;
ComputeIntLen[2] = &GClass::ComputeILI;
PostStep[0] = &GClass::PSS;
PostStep[1] = &GClass::PSE;
PostStep[2] = &GClass::PSI;
错误信息说得很清楚:
ISO c++禁止将、非限定或带括号的非静态成员函数的地址作为指向成员函数的指针。说,GClass: ComputeIntLenScattering
您还需要在typedefs中添加类:
typedef void (GClass::*ComputeIntLenFunc)(Int_t ntracks, Int_t *trackin, Double_t *lengths);
typedef void (GClass::*PostStepFunc)(Int_t ntracks, Int_t *trackin, Int_t &nout, Int_t* trackout);
那么你的(第一个)类型将意味着:这是一个指向类GClass的成员函数的指针,它接受Int_t
, Int_t*
等等,而在你的版本中,它只是引用了一个具有相同参数的自由函数。
- 如何在c++程序中找到函数的地址
- 函数名是c中该函数的第一条指令的地址吗
- 从 C++ 中的函数返回数组地址问题
- 为什么成员函数地址离自由函数这么远?
- 调用 lua 函数的地址为 C/C++?
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 为什么每次执行时函数的地址都不同?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 获取函数的地址?
- 模板到函数的时刻地址是否可以作为指向某个函数的函数指针传递?
- 为什么使用存储在虚拟方法表中的地址调用虚拟函数的函数会返回垃圾?
- 指针在函数调用后更改其地址
- 在 gcc/clang (C++) 中获取函数范围之外的标签地址
- 在每个运行时将函数保存到相同的地址
- 在指向现有内存地址的 hpp 文件中声明成员函数的最佳方法
- 创建一个函数来转换数组元素的类型并返回数组的地址
- 如何定义全局函数指针并分配给特定地址
- 常量引用函数参数的地址何时唯一?
- 打印我的Get MAC地址函数的std::字符串时出现意外输出
- 成员函数地址(函数指针)