1
這個問題的主要原因是,我想測試我的運營商overloader,而不必在我的單元測試期間做用戶輸入。我如何最好地實現這一目標?如何用cin單元測試代碼?
std :: istream & operator>>(istream &in, Fruit & f)
{
char temp[31];
in >> temp;
f.name = new char[strlen(temp) + 1];
strcpy(f.name, temp);
for(int i = 0; i < CODE_LEN; i++)
in >> f.code[i];
return in;
}
std :: ostream & operator<<(ostream &out, const Fruit & f)
{
out << setiosflags(ios::left) << setw(MAX_NAME_LEN) << f.name
<< " ";
for(int i = 0; i < CODE_LEN; i++) // why is this necessary?
out << f.code[i];
return out;
}