If I integrate a system with boosts odeint
module, using a class to define the derivative, the destructor of this class is called very often.
For example, this code:
#include <iostream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
class foo
{
public:
virtual ~foo() {
std::cout << "destructor called" << std::endl;
}
void operator()(const double &x, double &dxdt, double t) const {
dxdt = 1;
}
};
int main( int argc , char **argv )
{
double x = 0;
const double dt = 0.1;
typedef runge_kutta4< double > stepper_type;
integrate_const( stepper_type() , foo(), x , 0.0 , 10.0 , dt);
return 0;
}
calls the destructor around 400 times. (I'am a beginner in c++)
The destructor is only called once at the end of the program if
foo
in main and std::ref()
is used in the call to integrate_const()
Like this:
#include <iostream>
#include <boost/numeric/odeint.hpp>
#include <functional>
using namespace boost::numeric::odeint;
class foo
{
public:
virtual ~foo() {
std::cout << "destructor called" << std::endl;
}
void operator()(const double &x, double &dxdt, double t) const {
dxdt = 1;
}
};
int main( int argc , char **argv )
{
double x = 0;
const double dt = 0.1;
typedef runge_kutta4< double > stepper_type;
foo myfoo;
integrate_const( stepper_type() , std::ref( myfoo ), x , 0.0 , 10.0 , dt);
return 0;
}
You can allocate any kind of data dynamically in the foo class, for instance by adding a simple setter function .setData()
that could be called from main
with
myfoo.setData(myArray);
prior to the call to integrate_const()
.