指向不同 class 函数的函数指针数组
array of function pointer pointing to functions of a different class
我有一个 class MyClass。在其中,我想创建一个函数指针数组,并使用另一个 class (MemberClass) 的函数初始化该数组。
MyClass 有一个指向 MemberClass 作为成员的指针。
但是我得到了这个编译时错误
错误 C2440:'initializing':无法从 'void (__thiscall MyMemberClass::* )(void)' 转换为 'F'
//This class has the function that I want to create a function pointer to
class MemberClass
{
private:
int myValue1;
int myValue2;
public:
int GetValue1() //I want to point to this function from a function pointer in another class
{
return myvalue1;
}
int GetValue2() //I want to point to this function from a function pointer in another class
{
return myvalue2;
}
}
//This has array of function pointer that I want to point to member function of another class
Class MyClass
{
typedef void(*F)();
private:
MemberClass* mclass;
F[] f;
Process();
}
.cpp
MyClass::MyClass()
{
f[2] = {&MemberClass::GetValue1, &MemberClass::GetValue2} //This line throws error
//error C2440: 'initializing' : cannot convert from 'void (__thiscall MyMemberClass::* )(void)' to 'F'
}
void MyClass::Processing()
{
//This is how I am hoping to use the function pointer array
F[Index]();
}
F
声明为指向不带参数的函数的指针 returning void
。但是你的函数 return int
和 MemberClass
的成员函数而不是普通的普通函数。所以你需要的类型是
typedef int (MemberClass::*F)();
调用起来也比较有意思:
int result = (mclass->*f[index])();
建议:使用 C++11 的 functional
库而不是方法指针。
我稍微删减了 OP 的示例代码以简化示例。
MemberClass 基本保持不变。我删除了成员变量,因为这些方法现在被硬编码为 return 1 和 2 以便于区分它们。
#include <iostream>
#include <functional>
class MemberClass
{
public:
int GetValue1()
{
return 1;
}
int GetValue2()
{
return 2;
}
};
myClass
被撕毁了,因为这是行动所在。
class MyClass
{
private:
我正在使用 std::function
instead of a typedef and an array of the typedef. Note the template argument int()
. This is an array of functions that takes nothing and returns an int
. Magic in std::bind
will provide the hidden this
parameter required by methods. If the function has parameters that are not known at the time of binding, use std::placeholders
的数组来为它们在方法的参数列表中节省空间。
由于方法绑定到它们的对象,因此不再需要存储 MemberClass* mclass;
std::function<int()> f[2];
public:
调用函数很简单:索引数组并加上括号。
int Process(int index)
{
return f[index]();
}
根据您的思想流派,构造函数要么更棘手,要么不那么棘手。我正在使用初始化列表,因为它更简洁(无论如何对我而言)并且通常具有性能优势。一方面,您可以将数组换成 std::vector 或大多数其他容器,而无需更改变量定义以外的一行代码。
f[0] = std::bind(&MemberClass::GetValue1, mem);
f[1] =...
构造函数体内仍然是一个选项。
MyClass(MemberClass * mem):
f{std::bind(&MemberClass::GetValue1, mem),
std::bind(&MemberClass::GetValue2, mem)}
{
}
};
还有一些愚蠢的测试代码来确保这一切正常。为什么?因为每次您不以最简单的形式测试代码时,您都在冒不必要的风险。小的不行,大的也不行。
int main()
{
MemberClass c;
MyClass d(&c);
std::cout << d.Process(0) << std::endl;
std::cout << d.Process(1) << std::endl;
}
一个可剪切和粘贴的块全部加在一起:
#include <iostream>
#include <functional>
class MemberClass
{
public:
int GetValue1()
{
return 1;
}
int GetValue2()
{
return 2;
}
};
class MyClass
{
private:
std::function<int()> f[2];
public:
int Process(int index)
{
return f[index]();
}
MyClass(MemberClass * mem):
f{std::bind(&MemberClass::GetValue1, mem),
std::bind(&MemberClass::GetValue2, mem)}
{
}
};
int main()
{
MemberClass c;
MyClass d(&c);
std::cout << d.Process(0) << std::endl;
std::cout << d.Process(1) << std::endl;
}
我有一个 class MyClass。在其中,我想创建一个函数指针数组,并使用另一个 class (MemberClass) 的函数初始化该数组。 MyClass 有一个指向 MemberClass 作为成员的指针。 但是我得到了这个编译时错误 错误 C2440:'initializing':无法从 'void (__thiscall MyMemberClass::* )(void)' 转换为 'F'
//This class has the function that I want to create a function pointer to
class MemberClass
{
private:
int myValue1;
int myValue2;
public:
int GetValue1() //I want to point to this function from a function pointer in another class
{
return myvalue1;
}
int GetValue2() //I want to point to this function from a function pointer in another class
{
return myvalue2;
}
}
//This has array of function pointer that I want to point to member function of another class
Class MyClass
{
typedef void(*F)();
private:
MemberClass* mclass;
F[] f;
Process();
}
.cpp
MyClass::MyClass()
{
f[2] = {&MemberClass::GetValue1, &MemberClass::GetValue2} //This line throws error
//error C2440: 'initializing' : cannot convert from 'void (__thiscall MyMemberClass::* )(void)' to 'F'
}
void MyClass::Processing()
{
//This is how I am hoping to use the function pointer array
F[Index]();
}
F
声明为指向不带参数的函数的指针 returning void
。但是你的函数 return int
和 MemberClass
的成员函数而不是普通的普通函数。所以你需要的类型是
typedef int (MemberClass::*F)();
调用起来也比较有意思:
int result = (mclass->*f[index])();
建议:使用 C++11 的 functional
库而不是方法指针。
我稍微删减了 OP 的示例代码以简化示例。
MemberClass 基本保持不变。我删除了成员变量,因为这些方法现在被硬编码为 return 1 和 2 以便于区分它们。
#include <iostream>
#include <functional>
class MemberClass
{
public:
int GetValue1()
{
return 1;
}
int GetValue2()
{
return 2;
}
};
myClass
被撕毁了,因为这是行动所在。
class MyClass
{
private:
我正在使用 std::function
instead of a typedef and an array of the typedef. Note the template argument int()
. This is an array of functions that takes nothing and returns an int
. Magic in std::bind
will provide the hidden this
parameter required by methods. If the function has parameters that are not known at the time of binding, use std::placeholders
的数组来为它们在方法的参数列表中节省空间。
由于方法绑定到它们的对象,因此不再需要存储 MemberClass* mclass;
std::function<int()> f[2];
public:
调用函数很简单:索引数组并加上括号。
int Process(int index)
{
return f[index]();
}
根据您的思想流派,构造函数要么更棘手,要么不那么棘手。我正在使用初始化列表,因为它更简洁(无论如何对我而言)并且通常具有性能优势。一方面,您可以将数组换成 std::vector 或大多数其他容器,而无需更改变量定义以外的一行代码。
f[0] = std::bind(&MemberClass::GetValue1, mem);
f[1] =...
构造函数体内仍然是一个选项。
MyClass(MemberClass * mem):
f{std::bind(&MemberClass::GetValue1, mem),
std::bind(&MemberClass::GetValue2, mem)}
{
}
};
还有一些愚蠢的测试代码来确保这一切正常。为什么?因为每次您不以最简单的形式测试代码时,您都在冒不必要的风险。小的不行,大的也不行。
int main()
{
MemberClass c;
MyClass d(&c);
std::cout << d.Process(0) << std::endl;
std::cout << d.Process(1) << std::endl;
}
一个可剪切和粘贴的块全部加在一起:
#include <iostream>
#include <functional>
class MemberClass
{
public:
int GetValue1()
{
return 1;
}
int GetValue2()
{
return 2;
}
};
class MyClass
{
private:
std::function<int()> f[2];
public:
int Process(int index)
{
return f[index]();
}
MyClass(MemberClass * mem):
f{std::bind(&MemberClass::GetValue1, mem),
std::bind(&MemberClass::GetValue2, mem)}
{
}
};
int main()
{
MemberClass c;
MyClass d(&c);
std::cout << d.Process(0) << std::endl;
std::cout << d.Process(1) << std::endl;
}