class classe (){
public:
int key;
static void *funct(void *context){
printf("Output: %d, ", key);
}
void Go(){
classe c = static_cast<this>(context); //<- This doesn't work, Context of this-> goes here
pthread_create(&t, NULL, &classe::funct, c);
}
};
int main(){
classe c;
c.key = 15;
c.Go();
c.key = 18;
c.Go();
}
The output should be Output: 15, Output: 18,, the thing is to get context of this throws error.
Someone know how to fix this?
I can see a few problems with your code:
First,
static_cast<>requires a type in the<>, andthisacts like a variable (so not a type). The type ofthisisclasse*(pointer toclasseobject) withinclasse.Second, there is no
contextavailable inclasse:Go(). There is a parameter forclasse::fuct()by that name, but that is not available where you want to use it.Third,
pthread_create()assumes a free function (or a static member function) and you provide a class member function (classe::funct). Class memeber functions require an object to work on (sort of like an implicit parameter ==this). You also don’t have atdefined inclasse::Go()that you could pass topthread_create()You could try: