문제

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.

도움이 되었습니까?

해결책

With assignment:

funcPntr2 = funcPntr;

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top