我目前正在学习 C++,并且在转换指针方面遇到了一些问题。首先,我不确定我正在尝试做的事情就是我想做的事情。
我正在尝试采用一个函数,该函数可以根据字符串参数将适当的指针返回到各种方法指针,然后使用方法指针。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef void* (*plugin_function)(void*);
static class Plugin
{
public:
static bool doBoolStuff(){
return true;// A Simple function that returns true
}
};
void* getFunction(string pluginName, string functionName)
{
if(pluginName =="Bool"){
return &Plugin::doBoolStuff;
//If the string is right it should return a method pointer.
//I think that void* has the ability to point to anything, I am trying
//to use that functionality to create a system where I am able to set
//some parameters and get the appropriate method pointer.
}else{
return NULL;
}
}
int main(int argc, _TCHAR* argv[])
{
void* pluginFunction;
pluginFunction = getFunction("Bool","");
if(pluginFunction == &Plugin::doBoolStuff)cout<<"CastSuccesful!"<<endl;
//This section right here is where my code is breaking.
//
// IntelliSense: expression preceding parentheses of apparent call must have
//(pointer-to-) function type
//c:\Users\Walter\Dropbox\Inscription\MethodCasting\MethodCasting\MethodCasting.cpp
//MethodCasting
cout << "Bool function ->"<< pluginFunction()<<endl;
cout << "--------------------------------"<<endl;
system("pause");
}
任何反馈都会有所帮助。