C++中重载运算符的问题,求助`````

dirkjww 2010-03-08 06:44:35
看书过程中遇到不懂得代码 如下....

class CDate{
private:
int m_nDay;
int m_nMonth;
int m_nYear;
void AddDays(int nDayToAdd){
m_nDay+=nDayToAdd;
if(m_nDay>30){
AddMonths(m_nDay/30);
m_nDay%=30;
}
}
void AddMonths(int nMonthToAdd){
m_nMonth+=nMonthToAdd;
if(m_nMonth>12){
AddYears(m_nMonth/12);
m_nMonth%=12;
}
}
void AddYears(int nYearToAdd){
m_nYear+=nYearToAdd;
} //private中是定义的 日期加减 不管31天的实际
public:
CDate(int nDay,int nYear,int nMonth):m_nDay(nDay),m_nMonth(nMonth),m_nYear(nYear){}
CDate & operator ++(){ //为什么要用引用传递???
AddDays(1); //这个地方AddDays(1) 是对谁进行操作???
return *this; //这个地方的this 是指的?指 原对象?
}
CDate operator ++(int){ //传入的参数有什么作用???
CDate mReturnDate(m_nDay,m_nMonth,m_nYear);
AddDays(1); //为什么 在类中 对象用方法 不需要用 mReturnDate. 呢`??
return mReturnDate;
}
void DisplayDate(){
std::cout<<m_nDay<<"/"<<m_nMonth<<"/"<<m_nYear<<std::endl;
}
};




问题在 原代码中...求帮助啊....进行不下去了...在类中应用方法是不是和 在 函数中引用不一样???是不是有一个对象在里面??还是怎么弄的......没有实例化 对象....


...全文
104 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2010-03-08
  • 打赏
  • 举报
回复
const CDate operator ++(int)//你应该这么重载
Arikouch 2010-03-08
  • 打赏
  • 举报
回复
第一个引用是为了传入后改变值。
没有&的话,值是不会改变的
pengzhixi 2010-03-08
  • 打赏
  • 举报
回复
CDate NewDate=(mDate++)++; //你这里后置操作返回的是一个临时对象,你再对这个临时对象进行后置操作,自然不会有改变原来的对象啊
dirkjww 2010-03-08
  • 打赏
  • 举报
回复
刚刚 测试时 发现 新问题....
如下

int main(){
CDate mDate(25,6,2009);
mDate.DisplayDate();

CDate NewDate=(mDate++)++; //运行下来 结果是25/2009/6 26/2009/6 25/2009/6
mDate.DisplayDate();
NewDate.DisplayDate();
//如果用前置++ 则是 正确的 27/2009/6....???
}
dirkjww 2010-03-08
  • 打赏
  • 举报
回复
是的..这本烂书就没有讲this 指针....
也就是说在 类中 用到的 方法都 类似 可以看做是 this->function()?

CDate operator ++(int){
CDate mReturnDate(m_nDay,m_nMonth,m_nYear);
AddDays(1); //mReturnDate是函数名吧,不能这么用,需要用this指针
//怎么会是 函数名呢? 不是 复制了 一个对象 用来保存 ++之前的内容么???
return mReturnDate;
}

还有 引用返回 那个问题是?
可以
CDate A;
(A++)++...这样么`?我去试试
最帅马老师 2010-03-08
  • 打赏
  • 举报
回复
问题1: 使用引用传递就可以省去析构和构造的过程,效率高。尤其是返回this指针时。
问题2:

void AddDays(int nDayToAdd){
m_nDay+=nDayToAdd;
if(m_nDay>30){
AddMonths(m_nDay/30);
m_nDay%=30;
}
}


这段表明是对m_nDay直接进行操作并调用AddMonths函数
问题3: this是这个对象本身
问题4: 这个是地方并不是传入参数
问题5: mReturnDate是作为返回用
昵称很不好取 2010-03-08
  • 打赏
  • 举报
回复
楼主好好看看this指针吧,我感觉这点你有点没有掌握好
Good luck~~~
class CDate{
private:
int m_nDay;
int m_nMonth;
int m_nYear;
void AddDays(int nDayToAdd){
m_nDay+=nDayToAdd;
if(m_nDay>30){
AddMonths(m_nDay/30);
m_nDay%=30;
}
}
void AddMonths(int nMonthToAdd){
m_nMonth+=nMonthToAdd;
if(m_nMonth>12){
AddYears(m_nMonth/12);
m_nMonth%=12;
}
}
void AddYears(int nYearToAdd){
m_nYear+=nYearToAdd;
} //private中是定义的 日期加减 不管31天的实际
public:
CDate(int nDay,int nYear,int nMonth):m_nDay(nDay),m_nMonth(nMonth),m_nYear(nYear){}
CDate & operator ++(){ //为什么要用引用传递???
//传递引用的话,你就可以像cout<<xx<<yy<<endl;一样一直使用++操作符了
AddDays(1); //这个地方AddDays(1) 是对谁进行操作???
//这个函数相当于this->AddDays(1),当调用该函数的对象Days+1
return *this; //这个地方的this 是指的?指 原对象?
//this就是指调用这个函数的对象的指针,this相当于const CDate*指针,*this就是代表这个对象
}
CDate operator ++(int){ //传入的参数有什么作用???
//括号中的int,表示这个++是后置的++,因为++有两种,分别为a++和++a,这是规定写法
CDate mReturnDate(m_nDay,m_nMonth,m_nYear);
AddDays(1); //为什么 在类中 对象用方法 不需要用 mReturnDate. 呢`??
//mReturnDate是函数名吧,不能这么用,需要用this指针
return mReturnDate;
}
void DisplayDate(){
std::cout<<m_nDay<<"/"<<m_nMonth<<"/"<<m_nYear<<std::endl;
}
};
SiGoYi 2010-03-08
  • 打赏
  • 举报
回复
//为什么要用引用传递???
这个是返回值是一个CDate的引用,运算符重载都是这么做的
//这个地方AddDays(1) 是对谁进行操作???
这个是调用上面那个函数AddDays对日期进行加一。
//这个地方的this 是指的?指 原对象?
这个是相加后返回自己的指针,都是这么做的。
//传入的参数有什么作用??
传入几就加几天,上面的不是加一天么!你可以加几天都行。
//为什么 在类中 对象用方法 不需要用 mReturnDate. 呢`
上面是做个构造函数创建一个CDate类型,最后要把mReturnDate的结果返回。
pengzhixi 2010-03-08
  • 打赏
  • 举报
回复
CDate & operator ++(){ //为什么要用引用传递???
是返回引用,因为前自增的操作返回的是修改后的对象。
AddDays(1); //这个地方AddDays(1) 是对谁进行操作???
对this指针所指的对象进行修改。
return *this; //这个地方的this 是指的?指 原对象?
哪个调用这个操作符就指向哪个对象。
}
CDate operator ++(int){ //传入的参数有什么作用???
标准规定的,这个参数没用只是为了与前自增区别开
CDate mReturnDate(m_nDay,m_nMonth,m_nYear);
AddDays(1); //为什么 在类中 对象用方法 不需要用 mReturnDate. 呢`??
后自增返回的是自增前的对象,mReturnDate就是作为自增前的对象的拷贝。
return mReturnDate;
}

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧