通过类String看拷贝构造函数,赋值函数的作用和区别

/*如果不主动编写拷贝构造函数和赋值函数,编译器将以“位拷贝”的方式生成自动缺省函数.如果类中含有指针变量,这个缺省函数会带来错误.以类String的两个对象a,b为例,a.m_str内容是"hello",b.m_str的内容为"world".将a赋给b,缺省赋值函数的位拷贝意味着b.m_str=a.m_str,这样会发生如下3个错误:1. b.m_str的原有内存没有释放,造成内存泄露;2. b.m_str和a.m_str指向同一块内存,a或b的任一方变动均会影响到另一方;3. 两个对象被析构时,会造成m_str被析构两次*//*拷贝构造函数和赋值函数容易混淆,拷贝构造函数是对象被创建时调用的,赋值函数只能被已经存在了的对象调用*//* String a("hello"); String b("world"); String c= a;//调用拷贝构造函数,最好写成String c(a); c=b;//赋值函数*///测试函数void TestStucts();class String{ char* m_str;public: //构造函数 String(const char* str){ printf("普通构造函数被执行\n"); if(NULL==str){ m_str = new char[1]; m_str[0]='\0'; } else{ m_str = new char[strlen(str)+1]; strcpy(m_str,str); } } //拷贝构造函数 String(String& self){ printf("拷贝构造函数被执行\n"); m_str = new char[strlen(self.m_str)+1]; strcpy(m_str,self.m_str); } //赋值函数 String& operator=(const String& other){//引用不可能为null,不必判断这一层,构造函数则需要判断传入值 printf("赋值函数被执行\n"); if (this==&other)//1.检查自己给自己赋值 { //不做任何处理 } else { delete m_str;//2.释放自己原有内存 //3.分配新内存,并复制字符串.strlen返回有效字符串长度,不包含结束符'\0'。 //而strcpy则连结束符一起复制,因此不必担心新的被复制字符串中会缺乏结束符 m_str = new char[strlen(other.m_str)+1]; strcpy(m_str,other.m_str); } //4.返回对象的引用,为了实现链式表达式,千万不能写成return other, //因为other的生命周期不确定,甚至有可能是临时对象,赋值后会马上消失,那么return other返回的将是垃圾 return *this; } //析构函数 ~String(){ delete m_str;//内部数据类型,不用delete []m_str } void Show() { printf("text is:%s\n",m_str); }};示例程序://拷贝构造函数、赋值函数 String tmpstr = String("test"); tmpstr.Show(); String a("hello"); String b("world"); String c(a); c.Show(); c=b; c.Show();

猜你喜欢

转载自blog.csdn.net/shenhua969/article/details/24991387
今日推荐