C++,Bug 集中地:

1.cannot convert ‘this’ pointer from ‘const class Date’ to ‘class Date &’
原因:我开始以为是因为参数的const类型问题,就像char * 不能转成const char*。但是在实际代码调试中,发现问题出现在函数后的const上,const放在函数后面代表此函数不会改变成员变量。同时也是如果你函数体内部改变了变量值,则此const不能添加,例如:
bool Teacher::Iscontiwork()const{
int y=workdata.getyear();
if(Isretire() && 2016-y<35){
cout<<"continue work "<<endl;
cout<<“Name:”<<name<<endl;
cout<<“TeacherNum:”<<teachernum<<endl;
return 1;
}
return 0;
}
这个const就是有问题的。

2.这个是关于拷贝函数(const &)可以使用传进来的对象调用自己的私有变量,这是一个概念问题,可以参考这个博客,讲的比较好。
https://blog.csdn.net/weixin_40539125/article/details/84205194
3.对于运算符重载问题,() [] -> = ,重载则必须使用成员函数
friend 函数其实相当于不是类的函数,所以并不能调用自己的this,
重载运算符分为友元与非友元两种。
‘=’重载:
Date Date::operator = (const Date &d){
this->year=d.year;
this->month=d.month;
return *this;
}

word& word::operator++(){ //++a
++(this->record);
return *this;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值