虽然,我不会推荐这个解决方案,但如果你不想在你的代码中更改这一行:
read_ptr(buffer+(position++));
并且您仍然希望后递增position
2,然后将位置定义为此处定义Index position(2);
类型Index
的位置,并显示用法:
struct Index
{
int step;
int value;
Index(int s=1, int v=0): step(s), value(v) {}
Index operator++(int)
{
Index prev(step, value);
value += step;
return prev;
}
operator int() { return value; }
};
int main() {
char arr[] = "1234567890" ;
cout <<"Increment by 2" <<endl;
Index i2(2); //increment by 2
cout << *(arr + (i2++)) << endl;
cout << *(arr + (i2++)) << endl;
cout << *(arr + (i2++)) << endl;
cout << *(arr + (i2++)) << endl;
cout <<"Increment by 3" <<endl;
Index i3(3); //increment by 3
cout << *(arr + (i3++)) << endl;
cout << *(arr + (i3++)) << endl;
cout << *(arr + (i3++)) << endl;
cout << *(arr + (i3++)) << endl;
return 0;
}
输出:
Increment by 2
1
3
5
7
Increment by 3
1
4
7
0
工作示例:http: //ideone.com/CFgal
注意:我仍然不会在现实生活项目中建议这个解决方案。这更像是拼图:D