Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Feb 14, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Pointers to Class Member Functions

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?

You almost got it right. Try the following:

(this->*instructions[opcode])(arg1, arg2);
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date