Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call one of a member function pointer inside a struct

Tags:

c++

I have this following struct

typedef struct      s_ins
{
  std::string       item;
  int               extended;
  void              (CPU::*fc)(std::list<std::string> &list>;
}                  t_ins;

Then I declare an array, where my tokens need to be redirected in a function

t_ins     g_ins[] =                                                                                       
{                                                                                                         
  {"push", 1, &CPU::ins_push},                                                                            
  {"pop", 0, &CPU::ins_pop},                                                                              
  {"dump", 0, &CPU::ins_dump},                                                                            
  {"assert", 1, &CPU::ins_assert},                                                                        
  {"add", 0, &CPU::ins_add},                                                                              
  {"sub", 0, &CPU::ins_sub},                                                                              
  {"mul", 0, &CPU::ins_mul},                                                                              
  {"div", 0, &CPU::ins_div},                                                                              
  {"mod", 0, &CPU::ins_mod},                                                                              
  {"print", 0, &CPU::ins_print},                                                                          
  {"exit", 0, &CPU::ins_exit},                                                                           
  {"", 0, 0}                                                                                             
};

Finally this is how I call it (in a CPU function member) :

(*g_ins[i].fc)(tokens);

what I get is :

cpu.cpp: In member function ‘void CPU::get_instruction(std::list<std::basic_string<char> >&)’:
cpu.cpp:40:16: error: invalid use of unary ‘*’ on pointer to member

I tried many others way to call it, but I still have some errors. This is how I think : I access my function with g_ins[i].fc, tell him that it's a function member pointer with the *, and pass my parameters. I use it in a function of the same class of the function member pointer so I don't need to have a object before since it's 'this' right ?

Thanks

like image 555
Shinao Avatar asked Feb 26 '26 21:02

Shinao


1 Answers

A member function needs an object to be called on. When calling through a pointer to member function, you need to specify that object, even when you're calling it from another member function of that class. What you want instead is:

(this->*g_ins[i].fc)(tokens);

This uses the ->* operator to call the member function of CPU denoted by g_ins[i].fc on this.

Alternatively, if the functions don't depend on any object state, you could make them static and use normal function pointers. fc would now be defined as:

void (*fc)(std::list<std::string> &list>);

And you would call it like so:

g_ins[i].fc(tokens);
like image 125
Joseph Mansfield Avatar answered Mar 01 '26 11:03

Joseph Mansfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!