类的6个默认成员函数
对于一个没有成员的类来说,称之为空类;
但空类并非完全是空的,类会自动生成6个默认的成员函数:
构造函数
概念:构造函数是一种特殊的成员函数,它在创建对象时被调用,用于初始化对象的数据成员。构造函数和类名相同,并且没有返回类型,甚至不能包含return语句。
class Date { private: int _year; int _month; int _day; public: void Print() { cout << _year << "-" << _month << "-" << _day << endl; } }; int main() { Date d1; d1.Print(); }
像这种情况,我们没有创建一个构造函数,编译器会自动调用默认构造函数,
产生结果都是随机值;
class Date { private: int _year ; int _month; int _day; public: //无参构造函数 Date() { _year = 1; _month = 1; _day = 1; } //如果没有构造参数,编辑器会自动生成一个默认构造参数 void Print() { cout << _year << "-" << _month << "-" << _day << endl; } }; int main() { Date d1; d1.Print(); }
在我们的主程序中,我们没有使用我们的构造函数,但实际上,在我们创建d1对象时,编译器就已经开始调用我们的构造函数了;
创建类类型对象时由编译器自动调用,以保证每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。
也可将上面的无参构造参数改为这两种:
//全缺省参数构造函数 Date(int year = 2, int month = 2, int day = 2) { _year = year; _month = month; _day = day; } //普通的构造函数 Date(int year, int month, int day) { _year = year; _month = month; _day = day; } int main() { //调用带参数的构造函数,在创建对象的后面加上(...) Date d1(2023,10,24); }
这些都是构造函数,那有什么不同吗?
在这里,无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
当然,现在一些编译器也不让存在多个构造函数了:
所以,如果要自己实现构造参数的话,写一个即可;
而在C++11给出规定:C++11支持在内置类型变量声明处加上缺省值
我们也可以这么操作。
默认构造函数的用处:
C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类型,如:int/char…,自定义类型就是我们使用class/struct/union等自己定义的类型。
如果类中存在自定义类型的成员,可没有办法用过赋予缺省值来进行初始化,所以,可以通过调用默认构造参数,来实现对该类型进行初始化;
class Stack { private: int* _a; int _top; int _capacity; public: Stack(int capacity = 3) { cout << "Stack(int capacity = 3)" << endl; _a = (int*)malloc(sizeof(int) * capacity); if (_a == nullptr) { perror("Stack Fail"); exit(-1); } _top = 0; _capacity = capacity; } }; class MyQuene { private: Stack _PushSta; Stack _PopSta; int _size=20; public: void Print() { cout << "MyQuene Print" << endl; } }; int main() { Stack s1; MyQuene m1; return 0; }
析构函数
概念:析构函数是一种特殊的成员函数,它在对象被销毁时自动调用,用于执行清理操作和释放对象所占用的资源。析构函数的名称与类名相同,但前面需加上一个波浪线~作为前缀。
注意:如果没有明确的定义析构函数,编译器会提供一个默认的析构函数。默认析构函数不执行任何清理工作,仅释放对象所占内存空间。
class Date { private: //C++11支持在内置类型变量声明处加上缺省值 int _year = 1; int _month = 1; int _day = 1; public: Date(int year = 2, int month = 2, int day = 2) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } //析构函数 ~Date() { cout << "~Date()" << endl; } }; int main() { Date d1; return 0; }
默认析构函数的用处
class Stack { private: int* _a; int _top; int _capacity; public: Stack(int capacity = 3) { cout << "Stack(int capacity = 3)" << endl; _a = (int*)malloc(sizeof(int) * capacity); if (_a == nullptr) { perror("Stack Fail"); exit(-1); } _top = 0; _capacity = capacity; } //自定义一个析构函数 ~Stack() { cout << "~Stack()" << endl; free(_a); _a = nullptr; _top = _capacity = 0; } //默认的析构函数不会释放堆区上的空间,需要自己实现对资源的释放 }; class MyQuene { private: Stack _PushSta; Stack _PopSta; int _size=20; public: void Print() { cout << "MyQuene Print" << endl; } //让它默认生成析构函数 }; int main() { Stack s1; MyQuene m1; return 0; }
析构函数会在一个函数内,进行栈帧销毁之前将类对象进行析构;
拷贝构造函数
概念:拷贝构造函数是一种特殊的成员函数,用于创建一个新对象并将其初始化为已有对象的副本。只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
特征:
拷贝构造函数是构造函数的一个重载形式。
拷贝构造函数的参数只有一个且必须是类类型对象的引用
普通 情况下:
class Date { private: int _year; int _month; int _day; public: Date(int year = 2, int month = 2, int day = 2) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } ~Date() { cout << "~Date()" << endl; } void Func(Date d) { d.Print(); } }; class Stack { private: int* _a; int _top; int _capacity; public: Stack(int capacity = 3) { cout << "Stack(int capacity = 3)" << endl; _a = (int*)malloc(sizeof(int) * capacity); if (_a == nullptr) { perror("Stack Fail"); exit(-1); } _top = 0; _capacity = capacity; } //自定义一个析构函数 ~Stack() { cout << "~Stack()" << endl; free(_a); _a = nullptr; _top = _capacity = 0; } void Print() { cout << "Stack" << endl; } void Func(Stack* s) { s->Print(); } }; class MyQuene { private: Stack _PushSta; Stack _PopSta; int _size=20; public: void Print() { cout << "MyQuene Print" << endl; } //让它默认生成析构函数 }; int main() { Date d1(2023,10,22); d1.Func(d1); Stack s1; s1.Func(s1); }
原因:
所以,这里就有了拷贝构造函数这一函数;
写法:
Date(const Date& d) { cout << "Date(Date& d)" << endl; _year = d._year; _month = d._month; _day = d._day; }
Stack(Stack& s) { cout << "Stack(Stack& s)" << endl; _a = (int*)malloc(sizeof(int) * s._capacity); if (_a == nullptr) { perror("Stack Fail"); exit(-1); } _top = s._top; _capacity = s._capacity; } int main() { Stack s1; s1.Func(&s1); MyQuene q1; MyQuene q2(q1); }
由于d1对象已存在,所以对于形参d来说,就会自动调用拷贝构造函数;
注意:
测试:
Date Test(Date d) { Date dd(d); return dd; }
赋值运算符重载
概念:C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。(针对自定义类型)
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
不能通过连接其他符号来创建新的操作符:比如operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
在Date类中写:
//域内有一个隐藏的this指针,所以把第一个参数看作是this指针所指向的 bool operator>(const Date& y) { if (_year > y._year) { return true; } else if (_year == y._year && _month > y._month) { return true; } else if (_year == y._year && _month == y._month &&_day > y._day) { return true; } return false; }
测试:
Date d1; Date d2(2023, 10, 22); cout << (operator>(d1, d2)) << endl; //可以简写 cout << (d1 > d2) << endl;
d1被看作是this指针指向的对象,在写运算符重载函数时,可以省略第一个比较参数;
在实际上的比较操作时,可以像内置类型一样,直接使用运算符来进行比较;
对于赋值运算符重载,他是有默认函数的,也就是以值的方式逐字节拷贝;
默认情况下:
自己实现情况下:
//引用提高效率,由于主函数会有一个类对象来进行赋值,所以对象是存在的 Date& operator=(const Date& y) { if (this != &y)//如果是类对象对自己赋值,这种情况没必要 { _year = y._year; _month = y._month; _day = y._day; } return *this; }
赋值运算符只能重载成类的成员函数不能重载成全局函数
像这种赋值运算,对于一般情况,都是可以使用默认的赋值重载的,但如果类成员函数涉及到资源管理的话,就必须要自己实现;
取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date { private : int _year ; int _month ; int _day ; public : Date* operator&() { return this ; } const Date* operator&()const { return this ; } };