类的6个默认成员函数
在类中如果用户没有显示实现6个默认成员函数,编译器就会自动生成默认成员函数。
1.构造函数
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。
不同编译器自动调用的默认构造函数初始化值不同
在VS下的默认构造函数的初始值是随机值
#include <iostream>
using namespace std;
class Date
{
public:
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
d1.Print();
return 0;
}
构造函数的特性
构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
其特征如下:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
class Date
{
public:
// 1.无参构造函数
Date()
{}
// 2.带参构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1; // 调用无参构造函数
Date d2(2015, 1, 1); // 调用带参的构造函数
//Date d3();//error,如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
}
- 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
#include <iostream>
using namespace std;
class Date
{
public:
// 如果用户显式定义了构造函数,编译器将不再生成
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
// 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
// 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
// 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
Date d1;
return 0;
}
可以用全缺省的成员函数
#include <iostream>
using namespace std;
class Date
{
public:
// 如果用户显式定义了构造函数,编译器将不再生成
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
return 0;
}
- 编译器生成的默认构造函数会对自定义类型成员调用它的默认构造函数。(说明:C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类型,如:int/char…,自定义类型就是我们使用class/struct/union等自己定义的类型)
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
注意:C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在
类中声明时可以给默认值。
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 1970;//这里是声明,不是定义
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
- 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为
是默认构造函数。
2.析构函数
与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由
编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。
特性
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值类型。
- 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构
函数不能重载 - 对象生命周期结束时,C++编译系统系统自动调用析构函数。
#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 3)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (_array == nullptr)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
void Push(DataType data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
// 其他方法...
~Stack()//析构函数
{
if (_array)
{
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
int main()
{
Stack s;
s.Push(1);
s.Push(2);
}
- 编译器默认生成的默认析构函数,对自定类型成员调用它的析构函数,对于内置类型成员则销毁时不需要资源清理,最后系统直接将其内存回收即可
#include <iostream>
using namespace std;
class Time
{
public:
~Time()
{
cout << "~Time()" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 1970;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
- 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如
Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。
3.拷贝构造函数
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存
在的类类型对象创建新对象时由编译器自动调用。
注意:当你创建一个新对象并将其初始化为某个值时,如果使用了另一个同类型对象的值,那么会调用拷贝构造函数(如果适用)或移动构造函数(如果适用,并且对象是可移动的)。
特点
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,
因为会引发无穷递归调用(由于传值传参引发一个新的拷贝构造,从而导致无限拷贝,传引用传参就不会出现这种问题 )
传值传参会调用拷贝构造
#include <iostream>
using namespace std;
class Date
{
public:
void print()
{
cout << _year << "-" << _month << "-" << _day;
}
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// Date(const Date& d) // 正确写法
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
void func(Date d)//调用拷贝构造
{
d.print();
}
int main()
{
Date d1;
func(d1);
return 0;
}
#include <iostream>
using namespace std;
class Date
{
public:
void print()
{
cout << _year << "-" << _month << "-" << _day;
}
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// Date(const Date& d) // 正确写法
Date(const Date d) //错误写法,会导致无限递归
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);//拷贝构造
//Date d2 = d1;//拷贝构造
return 0;
}
- 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按
字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的
那么编译器自动生成了拷贝构造函数,我们还需要显示实现吗?
答案是要的,编译器自动生成的拷贝构造只会一个一个字节的去进行拷贝,这是浅拷贝,而有些拷贝仅靠浅拷贝是会出现问题的,这时候就要进行深拷贝(指的是在复制一个对象时,不仅仅复制对象的引用(即内存地址),而是递归地复制对象中的所有层级的属性(包括子对象)到新对象中,使得新对象和原始对象在内存中是完全独立的两个实体。这意味着对其中一个对象的修改不会影响到另一个对象,因为它们指向的是内存中的不同位置)。
比如:
#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 10)
{
_array = (DataType*)malloc(capacity * sizeof(DataType));
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
//注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
_size = 0;
_capacity = capacity;
}
void Push(const DataType& data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack()
{
if (_array)
{
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
Stack s2(s1);
return 0;
}
这里调用编译器自动生成的拷贝构造,是浅拷贝,对s2进行浅拷贝,导致s2和s1共用同一块空间,进行析构的时候就会对同一块空间进行两次析构,对一块内存空间多次释放会导致程序崩溃
那么栈的深拷贝要怎么写呢?
#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 10)
{
_array = (DataType*)malloc(capacity * sizeof(DataType));
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
//注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
_size = 0;
_capacity = capacity;
}
Stack(const Stack& st)
{
_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
_size = st._size;
_capacity = st._capacity;
}
void Push(const DataType& data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack()
{
if (_array)
{
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
Stack s2(s1);
return 0;
}
注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
拷贝构造函数典型调用场景:
- 使用已存在对象创建新对象
- 函数参数类型为类类型对象
- 函数返回值类型为类类型对象
下面我们来看一段代码
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
Date Test(Date d)
{
Date temp(d);
return temp;
}
int main()
{
Date d1(2022, 1, 13);
Test(d1);
return 0;
}
这里调用了1次构造函数,3次拷贝构造函数,4次析构函数
为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
尽量使用引用。
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
Date Test(Date& d)
{
Date temp(d);
return temp;
}
int main()
{
Date d1(2022, 1, 13);
Test(d1);
return 0;
}
可以看到运用引用拷贝构造和析构都减少了次数
运算符重载
C++为了增加代码可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
返回值类型。
- 函数名字为:关键字operator后面接需要重载的运算符符号。
- 函数原型:返回值类型 operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型参数
内置类型的比较并不需要进行运算符重载,因为编译器内部已经安排好了,对内置类型的大小比较
已经确定了,比如char类型的两个数据比较大小比较的是assic码值,并不需要进行运算符重载
- 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
- 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
藏的this
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 2024, int month = 9, int day = 7)
{
_year = year;
_month = _month;
_day = day;
}
bool operator==(const Date& d)//有隐含的this指针,形参比操作数目少1
{
//return this->_year == d._year
// && this->_month == d._month
// && this->_day == d._day;
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 13);
Date d3(d1);
Date d2(2021, 2, 3);
cout << d1.operator==(d3) << endl;//显示调用
cout << (d1 == d2) << endl;//转换调用,等价于上面的显示调用增加可读性
return 0;
}
- 一个类并不是所有操作符都需要重载,只需要重载有价值的操作符,比如两个日期相加就不需要进行重载,因为没有意义
- .* :: sizeof ?: . 注意以上5个运算符不能重载。
4.赋值运算符重载
赋值运算符重载格式
下面的T表示的是类类型
- 参数类型:const T&,传递引用可以提高传参效率,减少拷贝构造函数的调用
- 返回值类型:T&,返回引用可以提高返回的效率,传值返回会增加拷贝构造和析构的调用,还可以防止连续赋值造成的随机值问题。(引用返回:出了作用域,返回对象没有被析构,就可以用引用返回)
因为传值返回后创建临时对象进行返回,有返回值目的是为了支持连续赋值,和内置类型的连续赋值意思差不多,比如
int i = 0 ,j = 0, k = 3;
i = j = k;
这里j = k的返回值是int ,有返回值以便 i 的赋值,连续赋值
传引用返回
#include <iostream>
using namespace std;
class Date
{
public:
~Date()
{
cout << "~Date():" << this << endl;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date(int year = 2024, int month = 9, int day = 7)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
Date& operator=(const Date& d)//有返回值以便连续赋值,传引用返回提高效率,参数类型
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 13);
Date d2, d3;
d3 = d2 = d1;//连续赋值
d2.print();
d3.print();
return 0;
}
传值返回
#include <iostream>
using namespace std;
class Date
{
public:
~Date()
{
cout << "~Date():" << this << endl;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date(int year = 2024, int month = 9, int day = 7)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
Date operator=(const Date& d)//有隐含的this指针,形参比操作数目少1
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 13);
Date d2, d3;
d3 = d2 = d1;
d2.print();
d3.print();
return 0;
}
- 检测是否自己给自己赋值,
#include <iostream>
using namespace std;
class Date
{
public:
~Date()
{
cout << "~Date():" << this << endl;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date(int year = 2024, int month = 9, int day = 7)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
Date& operator=(const Date& d)//有隐含的this指针,形参比操作数目少1
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 13);
Date d3;
d3 = d1;
d3.print();
return 0;
}
- 返回*this :要复合连续赋值的含义
注意:拷贝构造和赋值运算符重载要区分的一个点:赋值调用的是赋值运算符重载的函数,而初始化调用的是拷贝构造函数
}
Date& test(int day)
{
Date tmp = *this;//调用的是拷贝构造
//Date tmp;
//tmp = *this;//调用的是赋值运算符重载函数
return *this;
}
注意:赋值运算符只能重载成类的成员函数不能重载成全局函数
用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
const成员
在了解const成员前,我们先来看一段代码
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
const Date d1(2024, 9, 8);
d1.print();
return 0;
}
可以发现代码会报错,由于d1对象是const类型的,而调用成员函数print()是,其中的参数为隐含的this指针,this的类型为Date* const this,传参权限放大,就会报错。
由于this指针是隐含的,不可以直接加const,为了弥补这个漏洞,于是就在成员函数后面加const,const对this指针进行修饰,表示const Date* const this,虽然权限缩小,但这是允许的
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
const Date d1(2024, 9, 8);
d1.print();
return 0;
}
5.取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date* operator&()
{
cout << "Date* operator&()" << endl;
return this;
}
const Date* operator&()const
{
cout << "const Date* operator&()const" << endl;
return this;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
int main()
{
Date d1;
const Date d2;
cout << &d1 << endl;
cout << &d2 << endl;
}