C语言中带有函数指针的函数数组在C ++中赋予成员函数 [英] Function array with function pointer in C assign to member function in C++

查看:141
本文介绍了C语言中带有函数指针的函数数组在C ++中赋予成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法将c ++类成员函数分配给c函数数组

我不确定我正在寻找的概念存在



I am looking for a way to assign a c++ class member function to c function array
I am not sure the concept that I'm looking for exists

class test
{
int id;
bool testid(int id1)
 {
  if(id>id1)
     return true;
   else
     return false;
 }
};
test item1, item2;
typedef bool (*test2) (int id);
test2 item3[2] = {&item1.testid, &item2.testid};



我不想以这种方式定义test2


I don't want to define test2 in this way

typedef bool (test::*test2) (int id);



非常感谢您的导游



我尝试了什么:



我试过


Many thanks for your guides

What I have tried:

I tried

void f1_0(int j);
void f1_1(int j);
typedef void (*f2)(int j);
f2 temp[2] = {&f1_0,&f2_0}



它有效


it works

推荐答案

语法不完全正确但它应该有效。请阅读main函数中的函数指针示例。 (其他的东西对你来说不是很有用)



这种结构的原因是什么?
The syntax isnt so fully correct but it should work. Read this function pointer example in the main function. (the other stuff isnt so useful for you)

What are the reasons for this weired construction?


它存在(如果我得到了你创建一个函数指针数组的可能性,例如

It exist (if I got you) the possibility to make an array of function pointers, e.g.
#include <stdio.h>

int inc(int i){ return i+1;}
int dec(int i){ return i-1;}

int main()
{
  int (*f[2])(int) = {inc,dec}; // array of function pointers

  int x = 5;
  printf("%d,%d,%d\n", x, f[0](x), f[1](x)); // call the functions assigned to the array items
  return 0;
}


这篇关于C语言中带有函数指针的函数数组在C ++中赋予成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆