visual c++void*为函数提供参数
visual c++ void* to parameter to a function
我在一些库中有这个函数:
class myConsole
{
void addCommand( std::string command, void* fn );
...
}
在我的课堂上,我有这样的功能:
void myApp::TestFn( const std::vector<std::string> & args )
{
// do something
}
在同一类中,我称之为:
void myApp::initApp( )
{
myConsole::getSingleton( ).addCommand( "FirstTest", &myApp::TestFn );
}
但这给了我一个错误:
错误c2664无法从'void(__thiscall)转换参数2myApp::*)(const std::vector<_Ty>&)'到'void*'
我该如何解决这个问题?
提前感谢!
您无法解决此问题。不能可靠地将函数指针强制转换为void *
并返回。
(我建议您重新设计程序并避开void*
;在C++中并没有真正的必要。)
这里的问题是,您试图将类方法作为void *
指针来传递。这是不可能的。
正确的方法是使用void addCommand (std::string, void *)
方法的模板。类似的东西
class myConsole {
template <typename T>
void addCommand( std::string command, T f);
};
struct Callback {
myApp &app;
Callback (myApp &a) : app(a) {
}
void operator() (const std::vector<std::string> &args) {
app.TestFn(args);
}
};
void myApp::initApp( )
{
myConsole::getSingleton( ).addCommand( "FirstTest", Callback(*this) );
}
这为您提供了C++中的回调原理,但我认为您需要比此解决方案更灵活的东西,因为您实际上想要自动选择将由回调执行的命令(在本例中为TestFn
)。
您应该避免使用void*
,尤其是在尝试使用函数指针时。我假设您只关注myApp
类中的成员函数指针,并且只对以const std::vector<std::string> &args
为参数的成员函数指示器感兴趣。这个typedef将创建适当的类型,并将其称为MemFunType
typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);
这里有一个完整的例子(在ideone上),其中有两个不同的成员函数你可能感兴趣,TestFn
和TestFnBackwards
。这个例子可能不是很有用,但它给出了一些成员函数指针的例子。
#include<iostream>
#include<vector>
using namespace std;
struct myApp;
struct myConsole
{
typedef void (myApp :: * MemFunType) (const std::vector<std::string> &);
void addCommand( std::string command, MemFunType fn );
};
struct myApp {
void TestFn( const std::vector<std::string> & args ) {
cout << " TestFn" << endl;
for(std :: vector<std::string> :: const_iterator i = args.begin(); i!=args.end(); i++) {
cout << *i << endl;
}
}
void TestFnBackwards( const std::vector<std::string> & args ) {
cout << " TestFnBackwards" << endl;
for(std :: vector<std::string> :: const_reverse_iterator i = args.rbegin(); i!=args.rend(); i++) {
cout << *i << endl;
}
}
static myApp & getSingleton();
} ma;
myApp& myApp :: getSingleton() {
return ma;
}
void myConsole :: addCommand( std::string , MemFunType fn ) {
vector<string> words;
words.push_back("hello");
words.push_back("world");
myApp &ma = myApp :: getSingleton();
(ma.*fn)(words); // execute the member on the singleton object, using the words as the argument.
}
int main() {
myConsole m;
m.addCommand( "FirstTest", &myApp::TestFn );
m.addCommand( "FirstTest", &myApp::TestFnBackwards );
}
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类