#include
#include
using namespace std;
class String{
private:
char*m_data;
public:
String(const char * str=NULL);
String(const String & other);
~String();
};
String::String(const char * str){
if(str!=NULL){
m_data=new char[strlen(str)+1];
strcpy(m_data,str);
}
else{
m_data=new char[8];
strcpy(m_data,"no char");
}
cout<<*m_data<<" constructing!"<<endl;
}
String::String(const String&other){
if(other.m_data!=NULL)
{
m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
}
else{
m_data=new char [8];
strcpy(m_data,"no char");
}
cout<<*m_data<<" copy constructing!"<<endl;
}
String::~String(){
cout<<*m_data<<" destructing!"<<endl;
delete[]m_data;
}
int main(){
String s1("family");
String s2=s1;
return 0;
}
你使用的是*m_data 而不是m_data