0

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.

cheezsteak
  • 2,731
  • 4
  • 26
  • 41

4 Answers4

1

Let me start off saying your design is broken. A static method that needs to access non-static members of a class and can't receive parameters?

That aside, sure you can. You can access a global object from inside the static method, that's set to the current object you're trying to manipulate:

extern nBody* currentBody;

//........
int nBody::accelerator()
{
    //access currentBody
    //since this is a member, you have access to other private members
}

//....
nBody someBody;
currentBody = &someBody;
nBody::accelerator();
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • The real question is who/how are nBody (non-standard class name) objects created? How are the masses defined? why can't the code that obviously has a reference to the nBody with the desired masses not invoke a non-static accelerator? – Jay Jul 03 '12 at 19:33
0

Given your constraints, a horrible solution would be to have a static variable in the class called something like currentNBody of type nBody * and set this to the appropriate instance before running the ODE solver.

You are correctly identifying this as a global variable. Of course this will fail utterly if you're doing anything multi-threaded.

Alex Wilson
  • 6,690
  • 27
  • 44
0

This is what i think you are talking about: Some processing is creating nBody objects with masses. Some other processing needs to 'accelerate' them. You need a way essentially to 'register' nBody objects with the 'acceleration process'. That could be a static method on the AccelerationProcess object or via some callback via RMI, etc. It is the accelerator process that holds in its state the nBody object references and thus no reason to have a static method on nBody. That is assuming the objects all live in the same memory space - otherwise things get more complicated.

Jay
  • 435
  • 3
  • 5
0

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82