M
ma740988
The conversion function ios_base:perator void* () calls good() on
its stream and returns the result.
I searched Dikumware but came up short, so I'm assuming operator void*
looks like
operator void*()
{
return (good() ? void*(this) : 0 );
}
Yes/No?
//// 2
Assume input_file has the contents
25 130 "Sarah"
class Test
{
public:
Test() { } ;
Test( int age_, int weight_, string name_)
: Age(age_)
, Weight(weight_)
, Name(name_)
{ } ;
~Test() { };
friend ostream& operator << ( ostream&, Test& );
friend istream& operator >> ( istream&, Test& );
void SaveAVec(Test &);
vector<Test> Employ1;
void WriteAllToScreen()
{
ifstream in("input_file.txt");
if (!in)
{
cerr << "error opening file... ";
exit(-1);
}
vector<Test>InVec;
Test Employ1;
while (in >> Employ1)
InVec.push_back(Employ1);
vector<Test>::iterator iter = InVec.begin(); // test
for ( ; iter != InVec.end(); ++iter )
cout << *iter << "\t";
in.close();
}
private:
int Age ;
int Weight;
string Name;
};
int main()
{
Test vec_test3;
//Test vec_test3(25, 124, "Sarah");
vec_test3.WriteAllToScreen();
//cout << vec_test3;
}
Extractors - as i understand it - parse information expected by the
destination object according to it's type. In this case the
information (25 130 "Sarah") and the type (Test) are two unrelated
entities, hence I'd expect the stream to fail. The fact that that
operator >> and subsequently the conversion operator got called for
Employ1 object - a UDT - is somewhat of a mystery to me. It further
leads me to believe that the Employ1 looks like Test Employ1(25, 130,
"Sarah"). Where have I gone wrong?
This makes sense to me
double f;
cin >> f;
assume f's input was 1.5. Now data and type match.
An aside: I realize the 'correct way' would be along the lines of ...
istream& operator >> ( istream& is, Test& rhs )
{
//cout << " istream& >>";
is >> rhs.Age;
is >> rhs.Weight;
is >> rhs.Name;
return is;
}
ostream& operator << ( ostream& os, Test& rhs )
{
//cout << "\t";
os << rhs.Age << '\n';
os << rhs.Weight << '\n';
os << rhs.Name << '\n';
return os;
}
its stream and returns the result.
I searched Dikumware but came up short, so I'm assuming operator void*
looks like
operator void*()
{
return (good() ? void*(this) : 0 );
}
Yes/No?
//// 2
Assume input_file has the contents
25 130 "Sarah"
class Test
{
public:
Test() { } ;
Test( int age_, int weight_, string name_)
: Age(age_)
, Weight(weight_)
, Name(name_)
{ } ;
~Test() { };
friend ostream& operator << ( ostream&, Test& );
friend istream& operator >> ( istream&, Test& );
void SaveAVec(Test &);
vector<Test> Employ1;
void WriteAllToScreen()
{
ifstream in("input_file.txt");
if (!in)
{
cerr << "error opening file... ";
exit(-1);
}
vector<Test>InVec;
Test Employ1;
while (in >> Employ1)
InVec.push_back(Employ1);
vector<Test>::iterator iter = InVec.begin(); // test
for ( ; iter != InVec.end(); ++iter )
cout << *iter << "\t";
in.close();
}
private:
int Age ;
int Weight;
string Name;
};
int main()
{
Test vec_test3;
//Test vec_test3(25, 124, "Sarah");
vec_test3.WriteAllToScreen();
//cout << vec_test3;
}
Extractors - as i understand it - parse information expected by the
destination object according to it's type. In this case the
information (25 130 "Sarah") and the type (Test) are two unrelated
entities, hence I'd expect the stream to fail. The fact that that
operator >> and subsequently the conversion operator got called for
Employ1 object - a UDT - is somewhat of a mystery to me. It further
leads me to believe that the Employ1 looks like Test Employ1(25, 130,
"Sarah"). Where have I gone wrong?
This makes sense to me
double f;
cin >> f;
assume f's input was 1.5. Now data and type match.
An aside: I realize the 'correct way' would be along the lines of ...
istream& operator >> ( istream& is, Test& rhs )
{
//cout << " istream& >>";
is >> rhs.Age;
is >> rhs.Weight;
is >> rhs.Name;
return is;
}
ostream& operator << ( ostream& os, Test& rhs )
{
//cout << "\t";
os << rhs.Age << '\n';
os << rhs.Weight << '\n';
os << rhs.Name << '\n';
return os;
}