Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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};

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
Posted
Updated 27-Nov-18 22:14pm
v3

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?
 
Share this answer
 
Comments
saide_a 27-Nov-18 7:35am    
I have a fixed function from a third party and I should assign to that
saide_a 28-Nov-18 4:08am    
I have edited my question please look at it
It exist (if I got you) the possibility to make an array of function pointers, e.g.
C
#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;
}
 
Share this answer
 
Comments
saide_a 28-Nov-18 4:08am    
I have edited my question please look at it
CPallini 28-Nov-18 4:52am    
"I don't want to define test2 in this way"
But there is NO another way (and you have to pass the pointer to the object when calling the functions). At least it is so until we are able to understand what you are really trying to do.
saide_a 28-Nov-18 6:04am    
I should make a fixed name function which its implementation is different for different instance( not a class instance). and the[fix name func] void(* function)(input) = &Myfucntion only work on none class method
CPallini 28-Nov-18 6:43am    
Could you please elaborate? What is the scenario? Have you to expose a C++ function to a C library? Details are needed.
saide_a 28-Nov-18 6:52am    
It is so hard to explain, I am using canopen of canfestival , it is written in C and I am trying to make each node generically

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900