C++基础问题,虚心请教大神

#include< iostream>
using namespace std;
class String
{
public:
String(char *str);
friend bool operator>(String &a, String &b);
void display();
private:
char *p;
};

String::String(char *str)
{
p=str;
}

bool operator>(String &a, String &b)
{
if (strcmp(a.p,b.p)>0)
return true;
else return false;
}

void String::display()
{
cout< < p ;
}
int main()
{String c("life"), d("lifestyle");
cout<<(c>d);

}
为什么会出现warming?
C:\Users\misuzu\Desktop\123\main.cpp|81|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

用最新的g++编译一下就会得到标题中的警告。
为什么呢?原来char *背后的含义是:给我个字符串,我要修改它。
而理论上,我们传给函数的字面常量是没法被修改的。
所以说,比较和理的办法是把参数类型修改为const char *。
这个类型说背后的含义是:给我个字符串,我只要读取它。

 #include< iostream>
using namespace std;
class String
{
public:
    String(const char *str);
    friend bool operator>(String &a, String &b);
    void display();
private:
    const char *p;
};

String::String(const char *str)
{
    p=str;
}

bool operator>(String &a, String &b)
{
    if (strcmp(a.p,b.p)>0)
        return true;
    else return false;
}


void String::display()
{
    cout<<  p   ;
}
int main()
{
    String c("life"), d("lifestyle");
cout<<(c>d);

}

同小白,我感觉是d("lifestyle")这里,引用一下这位大大的博文 http://blog.chinaunix.net/uid-26867468-id-3205532.html "lifestyle" 这个字符串程序没有给它分配空间,编译器把它分配到常量区.而常量字符串的值是不允许被修改的,所以这里的类型转换会被警告

strcmp的原型:int strcmp(const char *s1,const char * s2);
strcmp需要char *的实参,而你给是string类型的,可以使用string.c_str()完成string到char *的转换,也可以直接把代码中的所有string改成char *