前置++ 後置++

【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

相關文章
相關標籤/搜索