本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1. 构造函数
对于Date类,可以通过SetDate()公有的方法给对象设置内容,但是如果每次创建对象都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?
- 构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
class Date{
public:
void SetDate(int year, int month, int day){
_year = year;
_month = month;
_day = day;
}
void Display(){
cout <<_year<< "-" <<_month << "-"<< _day <<endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1,d2;
d1.SetDate(2018,5,1);
d1.Display();
Date d2;
d2.SetDate(2018,7,1);
d2.Display();
return 0;
}
特性:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
class Date {
public :
Date () {} // 1.无参构造函数
Date (int year, int month , int day ){ // 2.带参构造函数
_year = year ;
_month = month ;
_day = day ;
}
private :
int _year ;
int _month ;
int _day ;
};
int main(){
Date d1; // 调用无参构造函数
Date d2 (2015, 1, 1); // 调用带参构造函数
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
Date d3(); //声明了一个d3函数,该函数无参,返回一个日期类型的对象
}
- 构造函数的主要任务并不是开空间创建对象,而是初始化对象。
- 编译器生成的默认构造函数只会对自定义类型成员变量做处理。对内置类型(
int/char等)成员变量不做任何处理。 (对自定义类型成员变量(class/union/struct)的处理方式为调用其默认构造函数)
class A {
private:
int _hour;
int _minute;
int _second;
};
class Date {
public:
void Display() const {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
A time;
};
int main() {
Date d1;
d1.Display();
system("pause");
return 0;
}
- 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成这个无参的默认函数。
- ==无参==的构造函数和==全缺省==的构造函数都称为默认构造函数,并且默认构造函数只能有一个,如果显式定义多个就会报错,因为不知道调用何者。
注意:
3种默认成员函数:
- 无参构造函数
- 全缺省构造函数
- 未显式编写构造函数,编译器默认生成的构造函数
2. 析构函数
这时就引入了与构造函数相反的处理对象灭亡的析构函数。
析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。 而对象在销毁时会自动调用析构函数,完成类的一些资源清理的工作。
特性:
- 析构函数是特殊的成员函数。 其特征如下:
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值。
- 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
typedef int DataType;
class SeqList{
public:
SeqList(int capacity = 10){
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList(){
if (_pData){
free(_pData); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};
- 对象生命周期结束时,C++编译系统系统自动调用析构函数。
- 关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的 默认析构函数,对会自定类型成员调用它的析构函数。
class String{
public:
String(const char* str = "jack")
{
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
~String()
{
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
class Person{
private:
String _name;
int _age;
};
int main(){
Person p;
return 0;
}
3. 拷贝构造函数
拷贝构造函数只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且==必须使用引用==(&)传参,使用传值方式会引发无穷递归调用。 产生递归是因为构造这个形参对象还要调用拷贝构造
Date(const Date date){ //Date 类的拷贝构造函数
//TODO
}
int main(){
Date d2(d1) //用 d1 拷贝构造 d2
//Date date(d1);
//此时传值引发对象的拷贝,使用d1拷贝构造临时对象,又要调用拷贝构造函数
//一直无限递归下去
//...
}
- 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
class Date{
public:
Date(int year = 1900, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1;
// 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。
Date d2(d1);
return 0;
}
- 那么编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,像日期类这样的类没必要自己实现了。那么下面的类就出现了问题~
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
class String{
public:
String(const char* str = "jack"){
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
~String(){
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
int main(){
String s1("hello");
String s2(s1);
}
此时就要用户自己进行处理了,因为这涉及了深拷贝,如果使用系统默认提供的字节序的浅拷贝就无法满足了。
4. 赋值运算符重载函数
运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如
operator@。 - 重载操作符必须有一个类类型或者枚举类型的操作数。
- 用于内置类型的操作符,其含义不能改变,例如:内置的整型
+,不能改变其含义。 - 作为类成员的重载函数时,其形参看起来比操作数数目少一个,成员函数的操作符都有一个默认的形参
this,限定为第一个形参。 .*、::、sizeof、? :、.以上5个运算符不能重载。
// 全局的operator==重载
class Date{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//private:
int _year;
int _month;
int _day;
};
// 这里会发现运算符重载成全局的就需要成员变量是共有的,那么如何就遭到了破坏!
// 这里其实可以用 < 友元 > 解决,或者 < 重载成成员函数 >
bool operator==(const Date& d1, const Date& d2){
return d1._year == d2._year
&& d1._month == d2._month
&& d1._day == d2._day;
}
// 如何调用?
// bool operator==(Date* this, const Date& d2)
// 这里需要注意的是,左操作数是this指向的调用函数的对象
/*
bool operator==(const Date& d2){
return _year == d2._year
&& _month == d2._month
&& _day == d2._day;
}
*/
int main(){
Date d1(2018, 9, 26);
Date d2(2018, 9, 27);
cout << (d1 == d2) << endl;
}
运行结果:0
赋值运算符重载
class Date{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d){
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d){
if (this != &d){ //防止自己给自己赋值
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
赋值运算符主要有四点:
- 参数类型
- 返回值
- 检测是否自己给自己赋值
- 返回
*this - 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的浅拷贝。
在上面实现的情况下:
这里d1调用的编译器生成operator=完成拷贝,d2和d1的值也是一样的。
int main(){
Date d1;
Date d2(2018,10, 1);
d1 = d2; //赋值
return 0;
}
正是因为函数完成的是浅拷贝,那么像下面的示例中要完成的深拷贝问题,就无能为力了,程序会发生崩溃!
class String{
public:
String(const char* str = "jack")
{
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
~String(){
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
int main(){
String s1("hello");
String s2("world");
s1 = s2;
}
5. 普通对象与const对象取地址重载函数
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date{
public :
Date* operator&(){
return this ;
}
const Date* operator&()const{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况才需要重载
比如不想让别人获取到指定的内容,采用重载处理。
Date* operator&(){
return nullptr;
}
const Date* operator&() const{
return nullptr;
}
const修饰符
const修饰类的成员函数:
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
const如何修饰?
void Display() const {
cout << _year << endl;
cout << _day << endl;
}
//本质:
void Display(const Date* this){
cout << this->_year << endl;
cout << this->_day << endl;
}
所以这就完成了代码复用的功能。
class Date{
public:
void Display() {
cout << "Display ()" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
void Display() const {
cout << "Display () const" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
void Test(){
Date d1;
d1.Display();
const Date d2;
d2.Display();
}
结论:const成员调用const修饰函数,普通成员调用非const函数。
const修饰(对象与成员函数)的调用关系
const对象可以调用非const成员函数吗?不可以。非const对象可以调用const成员函数吗?可以。const成员函数内可以调用其它的非const成员函数吗?不可以。非const成员函数内可以调用其它的const成员函数吗?可以。
对象:
const对象不可以调用非const成员函数。
非const对象可以调用const成员函数、非const成员函数。
函数:
const成员函数内不可以调用其他的非const成员函数。
非const成员函数内可以调用其他的const成员函数、非const成员函数。
【小结】:
const类型不能调用非const类型。
因为const类型权限小,非const类型权限大。
const调用非const的行为相当于权限的放大,这是非法的。
非const调用const的行为相当于权限的缩小,这是允许的。