Callback of member functions through 3d party library

前端 未结 3 600
礼貌的吻别
礼貌的吻别 2020-12-22 10:16

I\'m working with a particle simulation library. The way interactions are added to particles it through the following library function:

AddInteraction(Partic         


        
3条回答
  •  半阙折子戏
    2020-12-22 10:57

    What you describe is not possible because the library does not know it has to pass this parameter to your member functions. You could do it if interaction accepted an argument reserved for the user. If you have a single object instance calling AddInteraction at any given time, then you can store a pointer to the instance:

    Object *Object::only_instance;
    
    void Object::AddInteractionCaller() {
       only_instance = this;
       AddInteraction(set, interaction_fn); 
    }
    
    void interaction_fn(xyz* p, xyz* v) {
      only_instance->interaction(p, v);
    }
    

提交回复
热议问题