name = new char[strlen(temp.name) + 1];
new的是数组,要用[]
class study {
public:
char* name, * url;
public:
study(const char* name, const char* url) {
this->name = new char[strlen(name) + 1];
this->url = new char[strlen(url) + 1];
if (this->name)
strcpy(this->name, name);
if (this->url)
strcpy(this->url, url);
}
study(study& temp) {
name = new char[strlen(temp.name) + 1];
url = new char[strlen(temp.url) + 1];
if (name)
strcpy(name, temp.name);
if (url)
strcpy(url, temp.url);
}
study& operator =(study& temp) {
delete[] name;
delete[] url;
name = new char[strlen(temp.name) + 1];
url = new char[strlen(temp.url) + 1];
if (name)
strcpy(name, temp.name);
if (url)
strcpy(url, temp.url);
return *this;
}
};
int main()
{
study a("试试", "www.shishi.com");
cout << a.name << ":" << a.url << endl;
study b = a;
cout << b.name << ":" << b.url << endl;
study c("看看", "www.kankan.com");
b = c;
cout << b.name << ":" << b.url << endl;
system("pause");
return 0;
}