C++拷贝构造&操作符重载
头文件 DString.h如下
#ifndef __DSTRING_H #define __DSTRING_H #endif #include <stddef.h> class DString { public: DString(const char * c = NULL); // default constructor DString(const DString &str); // copy constructor - implement DEEP copy ~DString(); /* 必须使用成员函数重载的运算符 = ( ) [ ] -> 必须使用友元函数重载的运算符 << >> P.S. 一目运算符,建议使用成员函数;二目运算符,建议只用友元函数 */ DString& operator = (const DString &str);
friend DString operator + (const DString &s1, const DString &s2);
char* dcopy(char * dst, const char * src); private: char * m_data; };
实现如下DString.cpp
#include <string.h> #include "DString.h" DString::DString(const char * c) { if(NULL == c) { m_data = new char('\0'); } else { m_data=new char[strlen(c) + 1]; dcopy(m_data, c); } } DString::DString(const DString &str) { if(NULL== str.m_data) { m_data=NULL; } else { m_data=new char[strlen(str.m_data) + 1]; dcopy(m_data, str.m_data); } } DString::~DString() { delete [] m_data; } DString& DString::operator = (const DString &str) { if(this == &str) // the same one { return *this; } else { delete [] m_data; m_data=new char[strlen(str.m_data)+1]; dcopy(m_data,str.m_data); return *this; } } // no :: because friend function doesnt belong to the class DString operator + (const DString &s1, const DString &s2) { if(NULL == s1.m_data) { return DString(s2); } if(NULL == s2.m_data) { return DString(s1); } char * temp = new char[strlen(s1.m_data) + strlen(s2.m_data) + 1]; strcpy(temp, s1.m_data); strcpy(temp+strlen(s1.m_data), s2.m_data); return DString(temp); } char* DString::dcopy(char * dst, const char * src) { char * tmp = dst; while(*src != '\0') { *tmp++ = *src++; } *tmp = 0; return dst; }
调用入口main.cpp
#include "DString.h" int main(int argc, char* argv[]) { DString s1("abcde"); //call default cons DString s2 = "ABC"; //call default cons DString s3 = s2; //call copy cons DString s4(s3); //call copy cons s1 = s4; //call operator = s4 = s1 + s2 + "ding"; //call default cons then + then + then = return 0; }
一些知识点(以在代码中出现的顺序排列)
- 对于一个空的类,默认会有6个成员函数:构造/析构/拷贝构造/赋值/const取址/非const取址
- 在函数声明时使用了默认参数,则在实现时就不能再次指定默认参数
- 对于基本类型(没有析构函数)释放内存时delete同delete[] (原则是与NEW NEW[]配对使用)
- 显示声明拷贝构造函数以实现深拷贝(否则自动生成浅拷贝版本),调用场景为:根据已有对象初始化新对象
- 显示声明赋值函数,若已实现深拷贝
- 重载二元操作符时,应使用友元函数(若使用成员函数实现则只需传递一个参数,另一个参数是自己)
- 友元函数/类,是通往私有成员的后门。其设计宗旨是,一种外部函数/类也可以访问类的私有成员。
- 友元函数不属于类,因此实现时,不需要也不能加::
- 友元函数不能使用this指针,因此使用时通常需要把类的对象作为参数传入
- 友元函数的典型使用场景是:一个函数需要访问多个类
- 重载+返回的是临时对象,因此不能函数返回值类型不能是引用
posted on 2017-03-05 17:15 Dean@cnbolg 阅读(157) 评论(0) 收藏 举报
浙公网安备 33010602011771号