1.类定义输入运算符 >> 时候,代码最后应该检测 输入流是否为有效,如果无效,应该把输入对象置于默认初始化状态。
定义>>时候,类必须是非常量引用。定义<<,最好是常量引用。
定义的时候不能加friend.
2.关于日期输入的正确格式:
Date & Date::operator=(const string &date)
{
istringstream is(date);
char ch1,ch2;
is >> year >> ch1 >> month >> ch2 >> day;
if(!is || ch1 != '-' || ch2 != '-' )
throw invalid_argument("Bad date");
if(month < 1 || month > 12 || day < 1 || day > 21)
throw invalid_argument("Bad date");
return *this;
}
3.
#include#include using namespace std; class Test { int &fun()const { return a; } private: int a; }; int main() { Test t1; return 0; }
binding reference of type ‘int&’ to ‘const int’ discards qualifiers
const成员函数不能返回类成员的非常量引用。因为有修改类成员的风险。



