如何使用结构内的功能指针调用私有函数
How to call a private function using a function pointer inside a structure?
我在下面提供了示例代码。我想使用功能指针从公共函数c::PubFn
调用c::LocFn2
。当我评论行时,pp[1].fp();
代码正常工作。请帮助我。
#include <iostream>
using namespace std;
class c
{
public:
void PubFn();
private:
struct p
{
int a;
int b;
int (c::*fp)();
};
static const p pp[];
int LocFn1();
int LocFn2();
};
void c::PubFn() {
cout << "Val = "<< pp[1].a << "n"; //It prints 3 correctly.
pp[1].fp(); //Here I wanna call c::LocFn2 using the function pointer.
}
int c::LocFn1() {
cout << "This is loc fn1n";
return 0;
}
int c::LocFn2() {
cout << "This is loc fn2n";
return 0;
}
const c::p c::pp[] = { {1, 2, &c::LocFn1}, {3, 4, &c::LocFn2} };
int main()
{
c obj;
obj.PubFn();
}
使用指针到会员操作员->*
。
(this->*(pp[1].fp))();
额外的括号是必要的
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?