C++ c++;动态引用函数指针
我有一个应用程序,其中需要完成以下任务C++ c++;动态引用函数指针,c++,C++,我有一个应用程序,其中需要完成以下任务 1.) UI application will send command code (integer value). 2.) DLL interface(in c++) will get that integer value and execute corresponding command function. 命令名称和命令代码保持为 #define PING 50 将有500个命令,应用开关箱听起来不太好。所以我决定在我的代码中实现函数指针,如下所
1.) UI application will send command code (integer value).
2.) DLL interface(in c++) will get that integer value and execute corresponding command function.
命令名称和命令代码保持为
#define PING 50
将有500个命令,应用开关箱听起来不太好。所以我决定在我的代码中实现函数指针,如下所示
#include "stdafx.h"
#include<iostream>
#define PING 20
using namespace std;
//extern const int PING = 10;
void ping()
{
cout<<"ping command executed";
}
void get_status(void)
{
cout<<"Get_status called"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)() );
};
void ToDoCommands::getCommand( void (*CommandToCall)())
{
void (*CommandToCall1)();
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
int code;
ToDoCommands obj;
cout<<"enter command code";
cin>>code; // if UI send 50 then Ping function get executed as #define PING 50
obj.getCommand(ping); // here m passing ping manually..
//obj.getCommand(get_status);
return 0;
}
您几乎做到了:将
std::string
的std::map
映射到函数指针,使用将字符串名称与相应函数指针配对的数据对其进行初始化,然后在运行时使用该映射根据传入的字符串参数选择正确的指针
#include <iostream>
#include <string>
#include <map>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
int main() {
map<string,void(*)()> m;
m["ping"] = ping;
m["test"] = test;
// I am using hard-coded constants below.
// In your case, strings will come from command line args
m["test"]();
m["ping"]();
return 0;
}
您就快到了:创建一个
std::string
到函数指针的std::map
,用数据将字符串名称与相应的函数指针配对来初始化它,然后在运行时使用该映射根据传入的字符串参数选择正确的指针
#include <iostream>
#include <string>
#include <map>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
int main() {
map<string,void(*)()> m;
m["ping"] = ping;
m["test"] = test;
// I am using hard-coded constants below.
// In your case, strings will come from command line args
m["test"]();
m["ping"]();
return 0;
}
我应该说@dasblinkenlight是正确的,但是如果您不想使用std::map,您应该自己实现一个map。这可能是有缺陷的,不是一种优化的方法,但是如果您不想使用STL,那么您应该自己实现它 可以使用两个具有相应索引的数组。其中一个是
char*
数组,另一个是函数指针。它们最好封装在一个名为MyMap的类中
class MyMap {
public:
...
inline void add(char *name, (void (*ptr)(void)) ) {
names_[currIndex_] = name; // Or stcpy
ptrs_[currIndex_] = ptr;
currIndex_++;
}
inline (void(*)(void)) get(char *name) {
int foundIndex = -1;
for (int i = 0; i < currIndex_; i++) {
// Find matching index
}
if (foundIndex_ >= 0) {
return ptrs_[foundIndex_];
}
return NULL;
}
private:
int currIndex_;
char *names_[10];
(void (*ptrs_[10])(void));
};
classmymap{
公众:
...
内联无效添加(字符*名称,(无效(*ptr)(无效))){
names_u0;[currendex_0;]=name;//或stcpy
ptrs_uu[currendex_u]=ptr;
CurrIndexuuuz++;
}
内联(void(*)(void))获取(char*name){
int foundIndex=-1;
对于(int i=0;i=0){
返回ptrs_u2;[foundIndex_2;];
}
返回NULL;
}
私人:
国际货币指数;
字符*名称[10];
(无效(*ptrs_u10])(无效);
};
我应该说@dasblinkenlight是正确的,但是如果您不想使用std::map,您应该自己实现一个map。这可能是有缺陷的,不是一种优化的方法,但是如果您不想使用STL,那么您应该自己实现它
可以使用两个具有相应索引的数组。其中一个是char*
数组,另一个是函数指针。它们最好封装在一个名为MyMap的类中
class MyMap {
public:
...
inline void add(char *name, (void (*ptr)(void)) ) {
names_[currIndex_] = name; // Or stcpy
ptrs_[currIndex_] = ptr;
currIndex_++;
}
inline (void(*)(void)) get(char *name) {
int foundIndex = -1;
for (int i = 0; i < currIndex_; i++) {
// Find matching index
}
if (foundIndex_ >= 0) {
return ptrs_[foundIndex_];
}
return NULL;
}
private:
int currIndex_;
char *names_[10];
(void (*ptrs_[10])(void));
};
classmymap{
公众:
...
内联无效添加(字符*名称,(无效(*ptr)(无效))){
names_u0;[currendex_0;]=name;//或stcpy
ptrs_uu[currendex_u]=ptr;
CurrIndexuuuz++;
}
内联(void(*)(void))获取(char*name){
int foundIndex=-1;
对于(int i=0;i=0){
返回ptrs_u2;[foundIndex_2;];
}
返回NULL;
}
私人:
国际货币指数;
字符*名称[10];
(无效(*ptrs_u10])(无效);
};
声明函数指针数组。将索引视为“代码”。例如:
void foo(){
printf("foo\n");
}
void bar(){
printf("bar\n");
}
int main(void)
{
void (*code_to_function[100])();
int code;
code_to_function[0] = foo;
code_to_function[1] = bar;
printf("Enter code: ");
scanf("%d", &code);
code_to_function[code]();
return 0;
}
请注意,对于这个基本示例,输入除0和1之外的整数代码将导致segfault 声明函数指针数组。将索引视为“代码”。例如:
void foo(){
printf("foo\n");
}
void bar(){
printf("bar\n");
}
int main(void)
{
void (*code_to_function[100])();
int code;
code_to_function[0] = foo;
code_to_function[1] = bar;
printf("Enter code: ");
scanf("%d", &code);
code_to_function[code]();
return 0;
}
请注意,对于这个基本示例,输入除0和1之外的整数代码将导致segfault 命令值已定义,或者您可以选择您喜欢的数字吗?是的,命令值已定义并修复是的,或者您可以选择您喜欢的数字吗?是的,命令值已定义并修复fixed@james什么意思?您已经使用了
string
s,添加map
没有什么问题。没有人知道你在dll中使用了STL。这个dll将与C#和IOs应用程序链接,而且它在linux环境下也不能工作…所以我需要数组而不是map@james需要数组而不是映射不是问题。你至少能使用std::pair吗?pair也属于STL…如果你能告诉我如何使用STL编码,我会很高兴的array@james:您是否可能不想在从DLL导出的函数中使用STL,因为STL执行一些内存分配/释放(跨越DLL边界的“否”)?如果是这样,您可以将STL作为DLL的实现细节,只需公开一个void invoke(const char*fn)
函数。@james您的意思是什么?您已经使用了string
s,添加map
没有什么问题。没有人知道你在dll中使用了STL。这个dll将与C#和IOs应用程序链接,而且它在linux环境下也不能工作…所以我需要数组而不是map@james需要数组而不是映射不是问题。你至少能使用std::pair吗?pair也属于STL…如果你能告诉我如何使用STL编码,我会很高兴的array@james:您是否可能不想在从DLL导出的函数中使用STL,因为STL执行一些内存分配/释放(跨越DLL边界的“否”)?如果是这样,您可以将STL作为DLL的实现细节,只需公开一个void invoke(const char*fn)
函数。