Instead myDataStream.get(myData), what you are doing is overloading operator>>for your data type:
std::istream& operator>>(std::istream& is, myDataStruct& obj)
{
return is;
}
If you want to read into an array, just write a loop:
for( std::size_t idx=0; idx<10; ++idx )
{
myDataStruct tmp;
if( is >> tmp )
myDataArray[idx] = tmp;
else
throw "input stream broken!";
}
Using the function template, you can also overload the operator for arrays on the right side (but I never tried this):
template< std::size_t N >
std::istream& operator>>(std::istream& is, myDataStruct (&myDataArray)[N])
{
}
But I can’t decide if this is magnificent or despicable.
source
share