Your code is written in a hybrid C/C++ style. For instance your destructor has a delete
(I can't find where the corresponding new
is) and that is basically never needed. Use a std::vector
to store array-like data.
Also you do a lot of parameter passing like void Nbody::update_position(Particle *p)
. Use references instead, and use const Particle &p
if the particle is only read.
Otherwise it looks like an n-body code to me. It's quadratic rather than something more sophisticated/efficient, but that's probably ok.
Oh, I've found the new
: you have
Particle *p = new Particle[n];
in the class definition, but n
is uninitialized. That is probably undefined behavior, definitely extremely dangerous, and most likely completely wrong.
Don't use new
to allocate an array! Use std::vector
, as follows:
std::vector<Particle> the_particles;
public:
Particles(int n) : the_particles(vector<Particle>(n)) {}
}```