const Date* p1:const修饰* p1
Date const* p2:const修饰* p2
Date* const p3:const修饰p3
结论:const在*之前修饰的是指针指向的对象,const在 *之后修饰的是指针本身
bool operator==(const Date* this,const Date& d)
const修饰的是this指向的对象,所以const在*之前
bool operator==(const Date& d) const //bool operator==(const Date* this,const Date& d)
{
return (_year == d._year)&&(_month == d._month)&&(_day == d._day);
}
bool operator==(const Date& d) const
黄色字体的const修饰*this,好处:函数中不小心改变的成员变量,在编译时会被检查出来
如下代码:若少了一个=,则会编译报错
bool operator==(const Date& d) const //bool operator==(const Date* this,const Date& d)
{
return (_year == d._year)&&(_month = d._month)&&(_day == d._day);
}