このようなクラス パーティクルがあります。
class Particle
{
public:
std::vector<Particle*> getNbh () const;
void setNbh (const std::vector<Particle*>&);
private:
std::vector<Particle*> nbh_;
};
そして機能Particle::setNbh(const std::vector<Particle*>&)
が実装され、
void Particle::setNbh (const std::vector<Particle*>& nbh)
{
nbh_ = nbh;
}
次に、非メンバー関数がありますupdateNeighbors (std::vector<Particle>& particles, double cutoff)
void updateNeighbors (std::vector<Particle>& particles, double cutoff)
{
for (auto particle : particles)
{
auto nbh = std::vector<Particle*>();
for (auto other : particles)
if (&particle != &other
&& norm(particle.getPosition() - other.getPosition()) < cutoff)
nbh.push_back(&other);
particle.setNbh(nbh);
}
}
問題は、この関数で隣人を更新すると、nbh_
メンバーが正しく更新されないことgetNbh()
です。各粒子のサイズを出力してテストします。
std::vector<Particle*>
目的の動作を得ることができるようにコピーを構築する正しい方法はどれですか?