简而言之,这就是一个对字符串字面量的理解的问题。字符串字面量不会被隐式转为
char *,而是
const char *,而下面的代码对其进行了隐式转换,转换完之后还进行了修改,显然是不对的。自然,编译运行出来的结果也是不对的。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 
 | #include <iostream>
 using namespace std;
 
 class str {
 private:
 char *w;
 int m, len;
 
 public:
 str(char *p, int n, int k) : w(p), m(n), len(k) {}
 void move();
 void show();
 };
 
 void str::move() {
 for (int i = 0; i < m; i++) {
 char first = *w;
 cout << w[0] << endl;
 for (int j = 1; j < len; j++) {
 w[j - 1] = w[j];
 }
 w[len - 1] = first;
 }
 }
 
 void str::show() {
 for (int i = 0; i < len; i++) {
 cout << *(w + i) << " ";
 }
 }
 
 int main() {
 str c("ABCD", 3, 4);
 c.move();
 c.show();
 return 0;
 }
 
 | 
一种可能的改法是,在调用构造函数的时候,传递一个数组,
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 
 | #include <iostream>
 using namespace std;
 
 class str {
 private:
 char *w;
 int m, len;
 
 public:
 str(char *p, int n, int k) : w(p), m(n), len(k) {}
 void move();
 void show();
 };
 
 void str::move() {
 for (int i = 0; i < m; i++) {
 char first = *w;
 cout << w[0] << endl;
 for (int j = 1; j < len; j++) {
 w[j - 1] = w[j];
 }
 w[len - 1] = first;
 }
 }
 
 void str::show() {
 for (int i = 0; i < len; i++) {
 cout << *(w + i) << " ";
 }
 }
 
 int main() {
 char buffer[] = "ACBD";
 str c(buffer, 3, 4);
 c.move();
 c.show();
 return 0;
 }
 
 | 
当然,如果依然想传字面量,那么,就可以改一下构造函数,把字面量复制出来,然后再修改即可。