Search code examples
c++classstatic-functions

Static member function with set parameters needs access to non-static data members


I have a static function that needs to access data members of a class. The function can be a member, non member or friend function of the class, but it must be static and it cannot take any arguments. So I cannot pass the data members to it as a parameter nor can I pass the object itself to it.

#include "sundials.h"
#include "CVode.h"

class nBody
{
private:
   double masses[];
   double** paths;
   static int accelerator();
   //...
public:
   //...
   void runODE();
};

int nBody::accelerator()
{
    // code that needs to know the values stored in masses[]
}
void nBody::runODE()
{
   //...
  ODEsetAccelerator(accelerator);  //require accelerator to be static int 
                                   //with specific parameters
   // run the ODE
   //record trajectories in paths[][]
}

accelerator is fed to a separate ODE solver which requires accelerator to be type static int and take specified arguments, So I can't pass the masses into accelerator because it will be called by the ODE and not main

is there any way I could make the accelerator function know what the value of masses? I don't care how indirect it is.


Solution

  • If this is the "sundials" and "CVode" that show up on a quick web search:

    Use the relevant nBody* as the user_data parameter.
    It's described in the documentation of the user function (page 55).

    If not, please disregard this answer.