Pointers to Class Member Functions

Question:
I’m trying to write a program that has a pointer to a private member function of a class. A public member function calls some of the private functions by using this pointer. I’m using a typedef for the pointer. The typedef is:

typedef void (pvm_em::*code_ptr)(int,int);

where pvm_em is the name of my class. I’m making an array of these pointers as follows:

code_ptr instructions[29];

and I’m filling this array by using statements that look like:

instructions[1] = &pvm_em::mov;

All of the code I’ve mentioned up to this point seems to work just fine, but I get a compiler error when I use the following line of code:

(this.instructions)[opcode](arg1, arg2);

I’ve tried several different ways of writing this line of code (with and without the This pointer and trying to dereference the pointer), but I can’t seem to get it to work. I get errors saying I must use the “.*” or “->*” operators. What are these operators, and what is the syntax for using them?

Answer:
You almost got it right. Try the following:

(this->*instructions[opcode])(arg1, arg2);

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: