Question

I have a C++ function pointer in a class ClassA:

void (ClassA::*funcPntr1)(void);

pointing to function:

void func();

by using assignment:

functPntr1 = &ClassA::func;

Now, i have another function pointer:

void (ClassA::*funcPntr2)(void);

I want funcPntr2 to point to the the function pointed by the funcPntr1. How do i do that?

I have tried this:

funcPntr2 = &(this->*funcPntr); //normally, i invoke the function pointer by (this->*funcPntr)()

But is wrong. I have tried many logical combinations, but doesn't seem to work.

Was it helpful?

Solution

With assignment:

funcPntr2 = funcPntr;

OTHER TIPS

If you want to assign the pointer which is currently assigned to it, it is a mere assignment, as others said (funcPntr2 = funcPntr).

If you want this assignment to persist, i. e. if you want that funcPntr2 always points to where funcPntr points to, even if the latter one changes, there is no way unless you change the definition.

You'd have to

  • have a void (ClassA::**funcPntr2)(void);
  • assign funcPntr2 = &funcPntr and
  • call (*funcPntr)().

As you talk about C++, I am not so familiar with; maybe you can do something with references.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top