1

I have problem using Borland C++ compiler in Eclipse, I want to pass address of one member function to constructor of another class which is friend of first class

PCB::PCB(Thread *t, long stack, int time, void (*method)()) 

I'm using method to find FP_SEG and FP_OFF, but it's giving me errors when I write

Thread::Thread (StackSize stackSize, Time timeSlice){ myPCB = new PCB(this, stackSize, timeSlice, run); } 

This is error I'm getting all the time:

Could not find a match for 'PCB::PCB(Thread * const,unsigned long,unsigned int,void)' in function Thread::Thread(unsigned long,unsigned int)

3
  • void (*method)() isn't a member function pointer. Commented May 15, 2016 at 22:28
  • But when I add (Thread::*method) FP_OFF(method) doesn't work anymore Commented May 15, 2016 at 22:31
  • I suggest reading this answer stackoverflow.com/a/7218633/5404527 Commented May 15, 2016 at 22:33

1 Answer 1

4

Although member function invocation looks like a regular function invocation, the two are not the same, because there is compiler trickery involved in passing the this pointer to the function being called. When you call a member-function from another member function, C++ fills in the this pointer for you. That is why member functions are incompatible with regular functions, only static functions are.

If you need to pass a function to an API that you did not write, you need to pass a regular function. Otherwise, consider changing the code to take std::function instead, because it is compatible with member functions.

Sometimes third-party C-style APIs support passing an extra parameter for the "context" of the call. If your API supports a void* as well, you could write a regular function that casts a void pointer coming back to an instance of your class, and call a member function on that instance.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.