8.1.4 运算符重载示例 例8.5:++运算符。 #include iostream.h class counter { public: counter() {value=0;} counter operator ++(); counter operator ++(int); void display() {coutvalueendl;} private: int value; }; counter counter::operator ++() { value++; 前缀运算符 后缀运算符 8.1.4 运算符重载示例(续) return *this; } counter counter::operator ++(int i) { counter t; t.value=value++; return t; } void main() { counter c; c.display(); for(int i=0;i10;i++) c++; c.display(); 后缀运算符变量值加1,表达式值不变 前缀运算符变量值与表达式值均加1 c.operator ++(0); 8.1.4 运算符重载示例(续) for(i=0;i10;i++)
原创力文档

文档评论(0)