I have been told it is good to overload the stream extraction operator to load data from a file. Would I simple use this in my class:
friend ifstream &operator>>( ifstream &input, Class &C )
{
input >> C.variable1 >> C.variable2 >> C.variable3;
return input;
}
What are the advantages of overloading the stream extraction operator to read data from a file? Also I am confused what to do in main. At the moment to read in the data I have for example:
ifstream fin;
fin.open("filename.txt");
if(fin.good())
{
object1 = new Class[10];
for (int i=0; i<10; i++)
{
fin >> object1[i].variable1;
fin >> object1[i].variable2;
fin >> object1[i].variable3;
}
}
Do I simple use something along the lines of:
Class::read(std::ifstream& input)
{
input >> object1;
}
Then call the function:
object1.read(filename)