c++有關lvaule
#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
01/09 22:58, 1F
→
01/09 22:58, , 2F
01/09 22:58, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章