-
类的6个默认成员函数
如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情 况下,都会自动生成6个默认成员函数:构造函数,析构函数,拷贝构造函数,赋值操作符重载函数,取地址操作符重载,const取地址操作符重载
-
构造函数
代码演示:
#include<iostream> using std :: cout; using std :: endl; class Date { public: void SetDate(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 d1; d1.Print(); //在没有初始化的情况下直接打印,会导致打印的全是随机数 d1.SetDate(1900, 1, 1); d1.Print(); }
输出结果为:
注意:如果对象没有初始化就直接使用会接出错,为了避免这种情况,所以每个类都会有一个默认的构造函数,使得类一旦被定义出来,就已经完成了初始化,并且构造函数可以自动生成,也可以自己创建2.1构造函数的特性:
构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主 要任务并不是开空间创建对象,而是初始化对象。
具体特征如下:
-
无返回值
-
函数名与类名相同
-
**对象实例化时编译器自动调用对应的构造函数。 **
-
构造函数可以重载
代码演示:
#include<iostream> using std :: cout; using std :: endl; class Date { public: //1.无参构造函数 Date() { cout<<"无参构造函数"<<endl; } //2.有参构造函数 Date(int year, int month, int day) { cout<<"有参构造函数"<<endl; _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { Date d1;//调用无参构造函数,d1后面不能加括号 d1.Print(); Date d2(1900,1,1);//调用有参构造函数 d2.Print(); //以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对像 Date d3(); //这句代码是错的,它的意思并不是在调用构造函数,而是声明d3这个函数,调用无参构造函数时,对象后不能加括号 }
输出结果为:
-
如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定 义编译器将不再生成
代码演示:
#include<iostream> using std :: cout; using std :: endl; class Date { public: /* 如果用户显式定义了构造函数,编译器将不再生成 Date(int year,int month,int day) { _year=year; _month=moth; _day=day; } */ void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { // 没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数 Date d1; d1.Print(); }
输出结果为:
注意:此时的结果竟然是随机数,这是什么原因呢?在我们自己没有创建构造函数的情况下,编译器应该会默认生成一个构造函数,但是很明显d1并没有进行初始化,难道是编译器没有默认生成构造函数吗?其实不是的,在我们自己没有创建构造函数的情况下,编译器其实默认生成了一个构造函数,但是由于它的一个特性,所以并没有初始化他的成员变量 -
编译器自己默认生成的构造函数只会初始化自定义类型成员变量,不会初始化内置类型的成员变量(内置类型就是语法已经定义好的类型:如 int/char…,自定义类型就是我们使用class/struct/union自己定义的类型)
代码演示:
#include<iostream> using std::cout; using std::endl; class Date2 { public: Date2() { cout << "Date2()" << endl; } private: int _year; int _month; int _day; }; class Date1 { public: void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; Date2 d2; }; int main() { Date1 d1; d1.Print(); }
输出结果为:
注意:很明显编译器默认生成的构造函数会对自定类型成员变量调用的它的默认成员函数,但是对内置变量则不会进行初始化以及其它操作,所以当一个类的成员变量是内置类型的时候,则必须要自己创建一个构造函数来进行初始化7.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参 构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数
代码演示:
#include<iostream> #include<stdlib.h> using std::cout; using std::endl; class Date { public: Date() {} Date(int year = 1990, 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; d1.Print(); system("pause"); }
输出结果:会报错,原因是出现了二义性,因为全缺省构造函数和无参数构造函数在主函数里面调用的时候都不需要实参,所以当这二者都出现在类里面的时候,在调用函数的时候就不知道到底调用哪个
-
-
析构函数
与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作
3.1析构函数的特性:
-
**析构函数名是在类名前加上字符 ~ **
-
无参数无返回值
-
一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数
-
对象生命周期结束时,C++编译系统系统自动调用析构函数
代码演示:
#include<iostream> using std::cout; using std::endl; typedef int SLDateType; class SqList { public: SqList(int capacity = 20) { _pdate = (SLDateType*)malloc(sizeof(capacity)*capacity); _size = 0; _capacity = capacity; } ~SqList() { if (_pdate) { free(_pdate); _pdate = NULL; _size = 0; _capacity = 0; } } private: int* _pdate; int _size; int _capacity; };
注意:当类的成员函数有在堆上申请空间的时候,则必须要自己创建一个析构函数用来释放空间避免内存泄漏,当类里面没有在堆上申请空间的时候,则没有必要自己创建析构函数,编译器自己默认创建的析构函数就可以清理资源,因为所有的成员变量和成员函数都是在栈上创建的空间,当函数运行完后,由于栈帧的原因,在栈上开辟的所有空间都会被系统自己清理掉
-
编译器生成的 默认析构函数,会对自定类型成员调用它的析构函数
代码演示:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<iostream> #include<stdlib.h> using std::cout; using std::endl; class string { public: string(const char* string = "hello world") { _str = (char*)malloc(strlen(string) + 1); strcpy(_str, string); } ~string() { free(_str); cout << "string()" << endl; } private: char* _str; }; class str { private: string _str; int _c; }; int main() { str s; }
输出结果为:
-
-
拷贝构造函数
只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象 创建新对象时由编译器自动调用
4.1构造函数的特性
-
拷贝构造函数是构造函数的一个重载形式
-
拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用
代码演示:
#include<iostream> #include<iostream> #include<stdlib.h> using std::cout; using std::endl; class Date { public: Date(int year, int month, int day) { _year = year; _month = month; _day = day; } Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } /* 拷贝构造函数的参数如果是传值,那么将会报错,因为会产生无穷递归 Date(const Date d) { _year = d._year; _month = d._month; _day = d._day; } */ void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { Date d1(1900,1,1); Date d2 = d1; d2.Print(); }
输出结果为:1900-1-1
注意:拷贝构造函数传值操作之所以会产生无穷递归是因为在栈帧上,到要向拷贝构造函数传参的时候,要将参数d1复制一份,此时就又会创建一个拷贝构造函数,这时还需要传参,所以需要将d1复制一份,那么就又得创建拷贝构造函数,以此往后不停地需要传参,也不停地需要创建新的拷贝构造函数,而传引用在传参的时候,就等于是将d1传了过去,不需要复制任何东西 -
若未显示定义,系统会生成默认的拷贝构造函数,但是这种默认的拷贝构造函数只能是浅拷贝,当我们需要深拷贝一段数据的时候,还需要自己来创建一个拷贝构造函数
代码演示:
#include<iostream> using std::cout; using std::endl; 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 d1(1900, 1, 1); Date d2 = d1; d2.Print(); }
输出结果为:1990-1-1
注意:当需要拷贝的内容不牵扯内存的时候,则用编译器默认生成的拷贝构造函数来进行浅拷贝是没有任何问题的,但是下面的代码在运行的时候则会崩溃
#include<iostream> using std::cout; using std::endl; class string { public: string(const char* str = "hello world") { _str = (char*)malloc(strlen(str) + 1); strcpy(_str, str); } ~string() { free(_str); _str = NULL; } /* 如果没有自己创建这个拷贝构造函数,那么程序就会崩溃 string(const string& str) { _str = (char*)malloc(strlen(str._str) + 1); strcpy(_str, str._str1); } */ void Print() { cout << _str << endl; } private: char* _str; }; int main() { string str1("hello"); string str2 = str1; str2.Print(); }
输出结果为:0
注意:想要代码成功运行只要放开我注释的哪一个部分即可,很明显涉及深拷贝的情况,编译器默认生成的拷贝构造函数将无法使用,需要自己手动创建一个
-
-
赋值运算符重载
5.1.运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类 型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
函数原型:返回值类型 operator操作符(参数列表)
注意:
1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
4.作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的 操作符有一个默认的形参this,限定为第一个形参
5.(.*) 、(:: ) 、(sizeof) 、(?:三目运算符) 、(.) 以上5个运算符不能重载。代码演示:
#include<iostream> using std::cout; using std::endl; class Date { public: Date(int year=1990,int month=1,int day=1) { _year = year; _month = month; _day = day; } void print() { cout << _year << "-" << _month << "-" << _day << endl; } int _year; int _month; int _day; }; //全局operator,类的成员变量必须是共有的 bool operator==(const Date& d1,const Date& d2) { return d1._year == d2._year && d1._month == d2._month && d1._day == d2._day; } int main() { Date d1(2019, 5, 14); Date d2; cout << (d1 == d2) << endl; }
注意:当operator处于全局的时候就不能再保持成员变量的安全性,所以operator一般是当做成员函数使用的
代码演示:
#include<iostream> using std::cout; using std::endl; class Date { public: Date(int year = 1990, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void print() { cout << _year << "-" << _month << "-" << _day << endl; } // bool operator==(Date* this, const Date& d2) // 这里需要注意的是,左操作数是this指向的调用函数的对象 bool operator==(const Date& d2) { return this->_year == d2._year && this->_month == d2._month && this->_day == d2._day; } private: int _year; int _month; int _day; }; int main() { Date d1(2019, 5, 14); Date d2; cout << (d1 == d2) << endl; }
5.2.赋值运算符重载
注意:
1.返回值是*this
2.赋值运算符重载用的是必须是两个已经存在的对象
3.一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝(浅拷贝)代码演示:
#include<iostream> using std::cout; using std::endl; class Date { public: Date(int year = 1990, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void print() { cout << _year << "-" << _month << "-" << _day << endl; } Date& operator=(const Date& d1) { this->_year = d1._year; this->_month = d1._month; this->_day = d1._day; return *this; } private: int _year; int _month; int _day; }; int main() { Date d1(2019, 5, 14); Date d2; d2 = d1; d2.print(); }
输出结果为:2019,5,14
注意:和拷贝构造函数一样,编译器默认生成的赋值运算符重载不能用以深拷贝,否则会导致程序崩溃
代码演示:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdlib.h> using std::cout; using std::endl; class string { public: string(const char* string = "hello world") { _str = (char*)malloc(strlen(string) + 1); strcpy(_str, string); } void print() { cout <<_str<< endl; } ~string() { free(_str); _str = NULL; } /* 如果没有自己传建一个深拷贝的赋值运算符重载,程序就会崩溃 string& operator=(const string& str1) { _str = (char*)malloc(strlen(str1._str) + 1); strcpy(_str, str1._str); return *this; } */ private: char* _str; }; int main() { string str1("hello"); string str2; str2 = str1; str2.print(); }
-
取地址及const取地址操作符重载
注意:这两个默认成员函数一般不用重新定义 ,编译器默认会生成
代码演示:
class Date { public: Date* operator&() { return this; } const Date* operator&() const { return this; } private: int _year; int _month; int _day; };
注意:这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比 如想让别人获取到指定的内容!(坑人专用)
代码演示:
#include<iostream> using std::cout; using std::endl; class Date { public: Date(int year = 1990, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date* operator&() { return 0x00000000; } private: int _year; int _month; int _day; }; int main() { Date d1; cout << &d1 << endl; }
输出结果为:
注意:很明显这个地址是我随意给的,并不是d1的实际地址 -
**const成员 **
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this 指针,表明在该成员函数中不能对类的任何成员进行修改
代码演示:
#include<iostream> using std::cout; using std::endl; class Date { public: void print() { cout << "print()" << endl; } void print() const { cout << "print() const" << endl; } private: int _year; int _month; int _day; }; int main() { Date d1; d1.print(); const Date d2; d2.print(); }
输出结果为:
结论:
1.const对象不可以调用非const成员函数
2.非const对象可以调用const成员函数- const成员函数内可以调用其它的非const成员函数
- 非const成员函数内可以调用其它的const成员函数
C++类与对象(中)---类的6个默认成员函数:构造函数,析构函数,拷贝构造函数,赋值运算符重载,取地址及const取地址操作符重载,const 成员
最新推荐文章于 2022-08-31 17:37:35 发布