C
Cleverbum
I have created a class Particle which has a method toString() that
prints out all the useful information. In my main program I create an
array of these objects and once I've fiddled with them a bit I want to
use the toString() method on them all.
Is there a way of defining toString() so that I don't have to loop
through all of the particles printing each individually.
The code might explain it better than I can so the relevant part of the
class is below:
class particle{
string toString(){
ostringstream os;
os << pos.toString << '\t'
<< Vel.toString << '\t'
<< Npos.toString << '\t'
<< w.toString << '\n'
return os.str();
} ;
}
I then create some particles:
particle* myParticles = new particle[n];
and would like to be able to write
cout << myParticles.toString();
rather than:
for(int i;i<n;i++){
cout << myParticles.toString();
}
prints out all the useful information. In my main program I create an
array of these objects and once I've fiddled with them a bit I want to
use the toString() method on them all.
Is there a way of defining toString() so that I don't have to loop
through all of the particles printing each individually.
The code might explain it better than I can so the relevant part of the
class is below:
class particle{
string toString(){
ostringstream os;
os << pos.toString << '\t'
<< Vel.toString << '\t'
<< Npos.toString << '\t'
<< w.toString << '\n'
return os.str();
} ;
}
I then create some particles:
particle* myParticles = new particle[n];
and would like to be able to write
cout << myParticles.toString();
rather than:
for(int i;i<n;i++){
cout << myParticles.toString();
}