c++有關lvaule

看板C_and_CPP (C/C++)作者 (nodePtt)時間16年前 (2010/01/09 22:51), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
#include <iostream> #include <cstring> using namespace std; class MyStr{ private: int length; char *buf; public: MyStr(const char* str){ length = strlen(str); buf = new char [length+1]; strncpy(buf, str, length+1); } MyStr(const MyStr & str){ length = str.length; buf = new char [length+1]; strncpy(buf, str.buf, length+1); } ~MyStr() { delete [] buf; } MyStr operator--(){ for(int i=0; i<length; i++){ char &ch = buf[i]; if(ch >= 'A' && ch <= 'Z'){ ch += 'a' - 'A'; break; } } return *this; } MyStr& operator--(int x){ for(int i=length-1; i>=0; i--){ char &ch = buf[i]; if(ch >= 'a' && ch <= 'z'){ ch += 'A' - 'a'; break; } } return *this; } void print() { cout << buf <<endl; } }; int main(){ MyStr my_str("Happy Birthday to Cathy"); my_str.print(); //line1 (--(my_str--)).print(); //line2 my_str.print(); //line3 ((--(my_str--))--------).print();//line5 my_str.print(); //line6 } /*我的問題是line2和line3相同, line5和line6為什麼不同? 希望有人能不煩解釋一下,謝謝。*/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.244.221

01/09 22:58, , 1F
為什麼line5和line6要相同? 那兩行看起來不一樣
01/09 22:58, 1F

01/09 22:58, , 2F
跑出來不一樣也不奇怪不是嗎? 除非有應該一樣的理由
01/09 22:58, 2F
文章代碼(AID): #1BI9VrEm (C_and_CPP)
文章代碼(AID): #1BI9VrEm (C_and_CPP)