拷贝构造函数
概念
用于将一个相同类型的对象内容拷贝到另一个对象中,只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象 创建新对象时由编译器自动调用。
举例如下
class date { public: //构造函数 date(int year = 2002, int month = 8, int day = 26) { _year = year; _month = month; _day = day; } //拷贝构造函数 date(const date& c)//也是构造函数的重载 { _year = c._year; _month = c._month; _day = c._day; } //打印函数 void Print() { cout << _year << "_" << _month << "_" << _day << endl; } private: int _year; int _month; int _day; };
这样我们对另一个对象进行初始化时就可以直接传来对象了.
在初始化时使用=赋值也可以调用到拷贝构造函数
其实我们不写拷贝构造函数我们的编译器也会给我们生成一个,自己生成的拷贝构造函数在浅拷贝的时候完全够用了,当我们需要深拷贝的时候就可以自己写.
深拷贝
既然我们不写拷贝构造函数编译器会自动生成一个用于浅拷贝的,为啥还要有呢?
主要是因为我们要写深拷贝.
深拷贝就是我们在实现的时候注意一下,不能简单通过赋值操作来拷贝的需要深拷贝一下.
比如指针等.
我们比如我们使用Stack类的时候需要从堆区拿空间,就需要指针来保存变量,如果我们使用浅拷贝就会造成free两次的警告,而且在使用的时候也十分诡异.
所以我们要专门写个拷贝构造函数来达到深拷贝的目的.
比如我们的Stack类的.
Stack(const Stack& s) { _data = (int*)malloc(sizeof(int) * (s._capacity)); memcpy(s._data, _data, sizeof(int) * s._capacity); _capacity = s._capacity; _top = s._top; }
我们的这些默认生成的函数都可以自己实现,只要自己记住他们的格式就好.
其中取地址重载等函数都可以实现但是,没必要编译器的实现以及足够我们使用了.
操作符重载
我们用类的时候总要使用-,+,=,*,/,++,--,==,!=等操作符,还是以日期类为例.
当然日期类就一般不会使用*,/了=.=
早操作符重载的格式如下
//返回值类型+operator+要重载的符号+(形参) Date& operator=(Date& d1 ,const Date& d2); //上方就是我对=号赋值符的一次实现.
而具体的我们要看实现.
比如我们可以选择在类里实现或是在类外实现.
先来一个日期类吧
class date { public: void Init(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _day; int _month; int _year; };
我们先用类外的方式实现一个==看看吧
bool operator==(const Date& d1, const Date& d2) { if (d1._year == d2._year && d1._month == d2._month && d1._day == d2._day) { return true; } else { return false; } }
这个其实并不能使用因为我们_year是私有形势存储的,要想使用就必须将_day,_month,_year公有化或者使用内部共有函数来得到他们的值,因为我们只是演示,所以我暂时先把他公有化出来.
我们实现好了这个==运算符后
上图框起的两种方式都可以使用到我们的==重载功能,但是我们偏向使用下面的a==b.
在我们使用a == b的时候其实编译器会帮我们换成第一种的形式—operator==(a,b).
不过我们还是把这些运算符放在类里较好,及保证了类的封装性,又保证了我们元素不被外界访问.
我们将操作符重载放在类里需要对格式进行稍微改变.
// ==运算符重载 bool operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) { return true; } else { return false; } }
在类内定义的格式其实和外面定义的几乎没啥区别,除了少了一个变量,在使用_year的时候我们不用把它变成共有化的了.
其实少的那个变量使用了this指针来代替.而_year其实也是通过this指针得到的.
但是我们的this是两个变量的那个呢?
来看看我们如何使用类里定义的重构函数就知道了.
我们在使用的时候的形势其实注定了,上面的①中的this其实就是a的地址.
而下面的②其实this也是a的地址.
两者在编译看来没有区别.编译器也会2变成1后,然后编译.
如果有两个变量一般是左边的左this指针,三个变量就是最左的是this指针
类比思考一下,我们的前置后置++ --等其实在类内实现的时候就没必要专门设置形参只需要一个this指针就够了.但是这样就没办法分别了,所以我们规定后置类型要创建一个int类型的形参用于函数分辨.以便形成重构.
下面是对日期类所有操作符重载的实现,我们在实现的时候,有些部分可以操作符可以复用最好复用,复用带来的好处有很多,不仅方便,而且后期找bug也可以减少低级错误.
class Date { public: void Print() { cout << _year << "_" << _month << "_" << _day << endl; } // 获取某年某月的天数 int GetMonthDay(int year, int month) { if (month > 12) { cout << "月输入错误" << endl; return -1; } int const arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//保存每个月的日期 if (month == 2 && JudgeLeapYear(year))//闰年 { return 29; } return arr[month]; } // 全缺省的构造函数 Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } // 拷贝构造函数 // d2(d1) Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } // 赋值运算符重载 // d2 = d3 -> d2.operator=(&d2, d3) Date& operator=(const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; } // 析构函数 ~Date() { ;//不写用默认的也可 } // 日期+=天数 Date& operator+=(int day) { *this = *this + day;//复用+ return *this; } // 日期+天数 Date operator+(int day) { Date tmp(*this); (tmp._day) += day; while (tmp._day >= GetMonthDay(tmp._year, tmp._month))//判断我们的day是不能超过当月的最大数值的. { tmp._day -= GetMonthDay(tmp._year, tmp._month); tmp._month += 1; if (tmp._month == 13) { tmp._month -= 12; tmp._year += 1; } } return tmp; } // 日期-天数 Date operator-(int day) { Date tmp(*this); (tmp._day) -= day; while (tmp._day <= 0) { tmp._day += GetMonthDay(tmp._year, tmp._month); tmp._month -= 1; if (tmp._month == 0) { tmp._month += 12; tmp._year -= 1; } } return tmp; } // 日期-=天数 Date& operator-=(int day) { _day -= day; while (_day <= 0) { _day += GetMonthDay(_year, _month); _month -= 1; if (_month == 0) { _month = 12; _year -= 1; } } return *this; } // 前置++ Date& operator++() { *this += 1; return *this; } // 后置++ Date operator++(int)//传的int是语法规定 { Date tmp(*this); *this += 1; return tmp; } // 后置-- Date operator--(int) { Date tmp(*this); *this -= 1; return tmp; } // 前置-- Date& operator--() { *this -= 1; return *this; } // >运算符重载 bool operator>(const Date& d) { if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) { return true; } else { return false; } } // ==运算符重载 bool operator==(const Date& d) { if (_year == d._year && _month == d._month && _day == d._day) { return true; } else { return false; } } //>=运算符重载 inline bool operator >= (const Date& d) { if (*this > d || *this == d) { return true; } else { return false; } } // <运算符重载 bool operator < (const Date& d) { return !(*this >= d); } // <=运算符重载 bool operator <= (const Date& d) { if (*this < d || *this == d) { return true; } else { return false; } } // !=运算符重载 bool operator != (const Date& d) { return !(*this == d); } // 日期-日期 返回天数 int operator-(const Date& d) { Date min = (*this > d ? d : *this); Date max = (*this > d ? *this : d); int ret = 0; while (min != max) { min++; ret++; } return ret; } private: bool JudgeLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 100 == 0) { return true; } else { return false; } } private: int _year; int _month; int _day; };
const成员
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this 指针,表明在该成员函数中不能对类的任何成员进行修改。
其实很简单,就是在类内函数()后加上const就可以把原来类型为Date*的this指针变成const Date*类型.