“error: invalid use of non-static data member ‘thread::tfun’”
Class thread {
typedef void* (th_fun) (void*);
th_fun *tfun;
void create(th_fun *fun=tfun) {
pthread_create(&t, NULL, fun, NULL);
}
}
How to have a function pointer inside a class?
Please note:- static deceleration will make the code compile. But my requirement is to hold the function per object.
Your use of pthreads is fine, and you have no pointer-to-member-function here.
The problem is that you’re trying to use a non-static member variable as a default parameter for a function, and you can’t do that:
The default argument must be something that’s — essentially — a global, or at least a name that doesn’t require qualification.
Fortunately it’s an easy fix!