C++ has it's own for_each, along with several containers that do what you require. Here's a cutdown example of your class using a std::vector -
Code:
#include <iostream> //include iostream header
#include <string> //include string header
#include <vector>
#include <algorithm>
using namespace std; //use the std namespace from this execution point forwards
class Person //define Person class object
{
protected:
string m_FirstName; //internal first name storage variable
string m_LastName; //internal last name storage variable
public:
Person(); //contructor
~Person(); //destructor //member functions
void SetLastName(string s_LastName); //function for setting last name
void SetFirstName(string);
friend ostream& operator << (ostream& os,const Person& p)
{
return os << p.m_FirstName << ' ' << p.m_LastName << " ,etc" << '\n';
}
};
Person::Person(void) //Person Constructor
{
//notify of construction
cout << "Constructing Person Object." << endl;
};
Person::~Person(void) //Person Destructor
{
//notify of desruction
cout << "Destructing Person Object." << endl;
};
void Person::SetFirstName(string s_FirstName) //function for setting first name
{
m_FirstName = s_FirstName;
return;
};
void Person::SetLastName(string s_LastName) //function for setting last name
{
m_LastName = s_LastName;
return;
};
template <class T>
class print
{
public:
ostream& os;
print(ostream& _os):os(_os){}
void operator ()(T& type)
{
os<< type;
}
};
int main()
{
Person me;
me.SetFirstName("Mike");
me.SetLastName("Tyson");
Person you;
you.SetFirstName("Lennox");
you.SetLastName("Lewis");
vector<Person> peeps;
peeps.push_back(me);
peeps.push_back(you);
//output to file by creating ofstream object rather
//than using cout
for_each(peeps.begin(),peeps.end(),print<Person>(cout));
return 0;
}