【发布时间】:2018-01-16 03:40:38
【问题描述】:
我了解如何制作前缀和后缀增量器。在我的班级 DoStuff 中,我有:
// Prefix; returns the incremented value
friend const DoStuff& operator++(DoStuff& a);
// Postfix: returns the incremented value
friend const DoStuff operator++(DoStuff& a, int);
在课外,我有
const DoStuff& operator++(DoStuff& a){
a.num++;
return a;
}
const DoStuff operator++(DoStuff& a, int){
DoStuff before(a.num);
a.num++;
return before;
}
分别用于前缀和后缀增量器。我不明白的是,C++ 怎么知道前者由++a 表示,而后者由a++ 表示。据我所知,前缀增量器引用了地址&,这在某种程度上意味着++ 运算符符号应该出现在它之前。
另外,我不太清楚为什么后缀需要int 变量。
【问题讨论】:
标签: c++ operator-overloading increment prefix postfix