前置++ 后置++

【1】后置++ios

#include <iostream>
using namespace std; int main() { cout << "Hello World" << endl; int a = 3; int b = 0; int c = 3; a = a++; a = a++; b = c++; cout << "a : " << a << endl; cout << "b : " << b << endl; cout << "c : " << c << endl; return 0; } // out /* Hello World a : 3 b : 3 c : 4 */

 【2】自定义类型c++

示例代码以下:this

#include <iostream>
using namespace std; class Age { public : Age(int v = 18) : m_i(v) {} Age& operator++() //前置++ 
 { ++m_i; return *this; } const Age operator++(int) // 后置++ 
 { Age tmp = *this; ++(*this);  // 利用前置++ 
        return tmp; } Age& operator=(int i) // 赋值操做 
 { this->m_i = i; return *this; } int value() const { return m_i; } private : int m_i; }; int main() { Age a; a = a++; cout << "a1 : " << a.value() << endl; Age b; b = a++; cout << "a2 : " << a.value() << endl; cout << "b1 : " << b.value() << endl; //(a++)++; //编译错误 //++(a++); //编译错误 //a++ = 1; //编译错误 
    (++a)++;   //OK 
    ++(++a);   //OK 
    ++a = 1;   //OK 
   return 0; } // out /* a1 : 18 a2 : 19 b1 : 18 */

 

Good Good Study, Day Day Up.spa

顺序 选择  循环 总结code

相关文章
相关标签/搜索