前置・後置のインクリメント、デクリメント/oprator++() operator--()
Accelerated C++の中で変数をインクリメントする場合で、その値を使用しない場合はすべて前置のインクリメント演算子が使用されています。
for (int i = 0; i < 10; ++i)
僕自身はC言語を覚えてから標準では後置のものを使用していますが、どちらが多いのでしょうか。
for (int i = 0; i < 10; i++)
これはやっぱり後置インクリメントの実装時の「歪」を敬遠したからなのですかね。ここでの「歪」とは後置演算子をオーバーロードする場合、意味のない引数が必要なことですが。
#include <iostream> class Test { public: Test() : n(0) { } void show() { std::cout << "value = " << n << std::endl; } Test& operator++() { std::cout << "++var is called" << std::endl; ++n; return *this; } Test& operator++(int arg) { std::cout << "var++ is called with arg = " << arg << std::endl; ++n; return *this; } private: int n; }; int main() { Test a; a.show(); ++a; a.show(); a++; a.show(); a.operator++(100); a.show(); }