运行无法达到效果
#include <iostream>
#include <string>
using namespace std;
class mystring{
private:
char *p;
public:
mystring(char *a){
p = a;
}
void set(char b){
p[0] = b;
}
void show(){
cout << *p << endl;
}
~mystring(){
delete[]p;
cout << "free memory!" <<endl;
}
};
int main() {
mystring s1("hello");
mystring s2 = s1;
s1.set('H'); // 修改字符串的第一个字符
s1.show();
s2.show();
return 0;
}
报错内容为[Warning] ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
想要的效果为
Hello
hello
free memory!
free memory!