调用指向成员函数的数组元素
Calling array elements that are pointer-to-member functions
我在 class A 中有一个指向成员函数的数组,需要访问 class B 中的元素。我的问题是在尝试访问时总是返回 1数组元素或类型彼此不匹配。
到目前为止我得到的是:
A.h:
#include <vector>
class A {
public:
typedef void(*func_ptr)(void);
A();
void func1();
void func2();
void func3();
std::vector<void(A::*)()> aFuncs;
private:
void appendFunc(void(A::*function)());
};
A.cpp
#include "A.h"
void A::func1 {...}
void A::func2 {...}
void A::func3 {...}
void A::appendFunc(void(A::*function)()) {
aFuncs.push_back(function);
}
A::A() {
appendFunc(&A::func1);
appendFunc(&A::func2);
appendFunc(&A::func3);
}
B.h
#include "A.h"
class B {
A a;
void test(int value);
};
B.cpp
#include "B.h"
void B::test(int value) {
// here i need to access the elements of the array aFuncs, so that i can
// call the functions of A
// something like
a.aFuncs[value];
}
这里的问题例如是,如果我这样使用它,a.aFuncs[value] 总是 returns 1。
到目前为止唯一对我有用的是:
void B::test(int value) {
typedef void (a::*fn)();
fn funcPtr = &a::func1;
(a.*funcPtr)();
}
但该解决方案并未使用数组,因此目前没有太大帮助。有人可以帮我解决这个问题吗?有什么基本的我不明白吗?
如果 aFuncs
是全局的,(a.*aFuncs[value])()
有效。在您的示例中,您应该能够执行 (a.*a.aFuncs[value])()
.
#include <iostream>
#include <array>
struct Foo {
int one() { std::cout << "one\n"; return 1; }
int two() { std::cout << "two\n"; return 2; }
int three() { std::cout << "three\n"; return 3; }
};
std::array<int(Foo::*)(), 3> a = {{
&Foo::one,
&Foo::two,
&Foo::three,
}};
int main() {
Foo f;
std::cout << "1: " << (f.*a[0])() << "\n";
std::cout << "2: " << (f.*a[1])() << "\n";
std::cout << "3: " << (f.*a[2])() << "\n";
}
输出:
one
1: 1
two
2: 2
three
3: 3
您需要使用 a
两次才能调用该函数。一次访问向量,然后再次调用函数。那个怪物看起来像
void B::test(int value) {
(a.*a.aFuncs[value])();
}
为了使其更简洁,您可以将函数指针复制到一个变量中,然后使用该函数指针调用该函数。看起来像
void B::test(int value) {
auto func = a.aFuncs[value];
(a.*func)();
}
你可以看到它在这个 Live Example 上工作。
如果您的编译器支持,您还可以利用 std::function 来使用替代方法:
std::array<std::function<void(Foo&)>, 3> a = {
&Foo::one,
&Foo::two,
&Foo::three
};
然后调用:
foo.a[0](foo);
我在 class A 中有一个指向成员函数的数组,需要访问 class B 中的元素。我的问题是在尝试访问时总是返回 1数组元素或类型彼此不匹配。
到目前为止我得到的是:
A.h:
#include <vector>
class A {
public:
typedef void(*func_ptr)(void);
A();
void func1();
void func2();
void func3();
std::vector<void(A::*)()> aFuncs;
private:
void appendFunc(void(A::*function)());
};
A.cpp
#include "A.h"
void A::func1 {...}
void A::func2 {...}
void A::func3 {...}
void A::appendFunc(void(A::*function)()) {
aFuncs.push_back(function);
}
A::A() {
appendFunc(&A::func1);
appendFunc(&A::func2);
appendFunc(&A::func3);
}
B.h
#include "A.h"
class B {
A a;
void test(int value);
};
B.cpp
#include "B.h"
void B::test(int value) {
// here i need to access the elements of the array aFuncs, so that i can
// call the functions of A
// something like
a.aFuncs[value];
}
这里的问题例如是,如果我这样使用它,a.aFuncs[value] 总是 returns 1。
到目前为止唯一对我有用的是:
void B::test(int value) {
typedef void (a::*fn)();
fn funcPtr = &a::func1;
(a.*funcPtr)();
}
但该解决方案并未使用数组,因此目前没有太大帮助。有人可以帮我解决这个问题吗?有什么基本的我不明白吗?
aFuncs
是全局的,(a.*aFuncs[value])()
有效。在您的示例中,您应该能够执行 (a.*a.aFuncs[value])()
.
#include <iostream>
#include <array>
struct Foo {
int one() { std::cout << "one\n"; return 1; }
int two() { std::cout << "two\n"; return 2; }
int three() { std::cout << "three\n"; return 3; }
};
std::array<int(Foo::*)(), 3> a = {{
&Foo::one,
&Foo::two,
&Foo::three,
}};
int main() {
Foo f;
std::cout << "1: " << (f.*a[0])() << "\n";
std::cout << "2: " << (f.*a[1])() << "\n";
std::cout << "3: " << (f.*a[2])() << "\n";
}
输出:
one
1: 1
two
2: 2
three
3: 3
您需要使用 a
两次才能调用该函数。一次访问向量,然后再次调用函数。那个怪物看起来像
void B::test(int value) {
(a.*a.aFuncs[value])();
}
为了使其更简洁,您可以将函数指针复制到一个变量中,然后使用该函数指针调用该函数。看起来像
void B::test(int value) {
auto func = a.aFuncs[value];
(a.*func)();
}
你可以看到它在这个 Live Example 上工作。
如果您的编译器支持,您还可以利用 std::function 来使用替代方法:
std::array<std::function<void(Foo&)>, 3> a = {
&Foo::one,
&Foo::two,
&Foo::three
};
然后调用:
foo.a[0](foo);