类的六个默认成员函数
如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们什么都不写的情况下,都会自动生成下面六个成员函数。
class Empty{
public:
Empty(); // 构造函数
~Empty(); // 析构函数
Empty(const Empty&); //拷贝构造函数
Empty& operator=(const Empty&); //赋值运算符重载函数
Empty* operator&(); //普通对象取地址操作符重载函数
const Empty* operator&() const; //const对象取地址操作符重载函数
};
构造函数
构造函数是特殊的成员函数,构造函数虽然名叫构造,但是它的功能并不是开空间创建对象,而是初始化对象。
主要特点
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
- 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
- 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、用户未显式定义编译器默认生成的构造函数,都可以认为是默认构造函数。默认构造函数是指不用传参的构造函数。通过无参构造函数实例化对象时,对象后面不用跟(),否则就成了函数声明。
- 用户未显式定义构造函数,编译器自动生成的默认构造函数。注意:编译器生成的默认构造函数只对自定义类型成员变量做处理,对内置类型成员变量不做任何处理。对自定义类型成员变量的处理方式为调用其默认构造函数(如果该自定义类型没有默认构造函数,只有构造函数会报错)。
- 成员变量的命名风格。变量名前面或后面加_或者前面加m_。
代码演示
通过无参构造函数实例化对象时,对象后面不要跟()
#include <iostream>
using std::cout;
using std::endl;
class Date{
public:
Date(){
}
void Display() const{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1();
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
date.cc: In function ‘int main()’:
date.cc:22:5: error: request for member ‘Display’ in ‘d1’, which is of non-class type ‘Date()’
d1.Display();
^
只能有一个默认构造函数
#include <iostream>
using std::cout;
using std::endl;
class Date{
public:
Date(){
}
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;
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
date.cc: In function ‘int main()’:
date.cc:23:7: error: call of overloaded ‘Date()’ is ambiguous
Date d1;
^
date.cc:23:7: note: candidates are:
date.cc:10:3: note: Date::Date(int, int, int)
Date(int year = 1900, int month = 1, int day = 1){
^
date.cc:7:3: note: Date::Date()
Date(){
^
有歧义,实例化对象不确定该调哪个默认构造函数。
编译器生成的默认构造函数只对自定义类型成员函数进行处理
#include <iostream>
using std::cout;
using std::endl;
class Time{
public:
Time(){
cout << "Time Constructor!" << endl;
}
private:
int _hour;
int _mintue;
int _second;
};
class Date{
private:
int _year;
int _month;
int _day;
Time _time;
};
int main(){
Date d1;
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
Time Constructor!
析构函数
通过对构造函数的了解,我们知道一个对象怎么来的,那一个对象又是怎么没的呢?
析构函数的功能与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源的清理工作。
主要特点
- 析构函数名是在类名前加上~。
- 没有参数,没有返回值。
- 一个类只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
- 对象声明周期结束时,C++编译系统会自动调用析构函数。
- 用户未显式定义,编译器默认生成的析构函数对内置类型成员变量什么操作都不做,对自定义类型成员变量进行处理。处理方式为调用自定义类型成员变量的析构函数。
代码演示
对象声明周期结束,自动调用析构函数
#include <iostream>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
~Date(){
cout << "Destructor!" << endl;
}
void Display() const{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1;
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2000-1-1
Destructor!
编译器默认生成的析构函数对内置类型的处理
#include <iostream>
using std::cout;
using std::endl;
class Time{
public:
~Time(){
cout << "Time Destructor!" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
~Date(){
cout << "Date Destructor!" << endl;
}
void Display() const{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
Time _time;
};
int main(){
Date d1;
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2000-1-1
Date Destructor!
Time Destructor!
拷贝构造函数
拷贝构造函数,只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
主要特点
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归。
- 若用户未显式定义,编译器生成默认的拷贝构造函数。默认的拷贝构造函数按字节序完成拷贝,这种拷贝也叫浅拷贝或者值拷贝。像前面演示的日期类使用浅拷贝完全就够了,但是如果类中有动态开辟的空间,浅拷贝就会造成两个对象指向同一块空间。如果两个对象都在生命周期结束时,对其进行释放,会出问题。一块动态开辟的空间不能释放两次。因为第一次释放已经将其还给操作系统,如果操作系统将其分配给其他用户,这时候再次释放,就是在释放别人申请的空间,这显然是不合理的。
代码演示
拷贝构造函数传值
#include <iostream>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, 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;
}
void Display(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1;
d1.Display();
Date d2(d1);
d2.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
date.cc:13:20: error: invalid constructor; you probably meant ‘Date (const Date&)’
Date(const Date d){
^
编译器默认生成的拷贝构造函数
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
test = (int*)malloc(sizeof(int));
_year = year;
_month = month;
_day = day;
}
~Date(){
free(test);
cout << "Date Destructor!" << endl;
}
void Display(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int* test;
int _year;
int _month;
int _day;
};
int main(){
Date d1;
d1.Display();
Date d2(d1);
d2.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2000-1-1
2000-1-1
Date Destructor!
*** Error in `./date': double free or corruption (fasttop): 0x000000000213c010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81489)[0x7fefa7826489]
./date[0x400aeb]
./date[0x4009fe]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fefa77c73d5]
./date[0x4008c9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fd:01 1310735 /home/sss/prictice/C++/class_and_obj/date
00601000-00602000 r--p 00001000 fd:01 1310735 /home/sss/prictice/C++/class_and_obj/date
00602000-00603000 rw-p 00002000 fd:01 1310735 /home/sss/prictice/C++/class_and_obj/date
0213c000-0215d000 rw-p 00000000 00:00 0 [heap]
7fefa0000000-7fefa0021000 rw-p 00000000 00:00 0
7fefa0021000-7fefa4000000 ---p 00000000 00:00 0
7fefa77a5000-7fefa7967000 r-xp 00000000 fd:01 658657 /usr/lib64/libc-2.17.so
7fefa7967000-7fefa7b67000 ---p 001c2000 fd:01 658657 /usr/lib64/libc-2.17.so
7fefa7b67000-7fefa7b6b000 r--p 001c2000 fd:01 658657 /usr/lib64/libc-2.17.so
7fefa7b6b000-7fefa7b6d000 rw-p 001c6000 fd:01 658657 /usr/lib64/libc-2.17.so
7fefa7b6d000-7fefa7b72000 rw-p 00000000 00:00 0
7fefa7b72000-7fefa7b87000 r-xp 00000000 fd:01 675542 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fefa7b87000-7fefa7d86000 ---p 00015000 fd:01 675542 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fefa7d86000-7fefa7d87000 r--p 00014000 fd:01 675542 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fefa7d87000-7fefa7d88000 rw-p 00015000 fd:01 675542 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fefa7d88000-7fefa7e89000 r-xp 00000000 fd:01 658665 /usr/lib64/libm-2.17.so
7fefa7e89000-7fefa8088000 ---p 00101000 fd:01 658665 /usr/lib64/libm-2.17.so
7fefa8088000-7fefa8089000 r--p 00100000 fd:01 658665 /usr/lib64/libm-2.17.so
7fefa8089000-7fefa808a000 rw-p 00101000 fd:01 658665 /usr/lib64/libm-2.17.so
7fefa808a000-7fefa8173000 r-xp 00000000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
7fefa8173000-7fefa8372000 ---p 000e9000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
7fefa8372000-7fefa837a000 r--p 000e8000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
7fefa837a000-7fefa837c000 rw-p 000f0000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
7fefa837c000-7fefa8391000 rw-p 00000000 00:00 0
7fefa8391000-7fefa83b3000 r-xp 00000000 fd:01 658372 /usr/lib64/ld-2.17.so
7fefa85a7000-7fefa85ac000 rw-p 00000000 00:00 0
7fefa85af000-7fefa85b2000 rw-p 00000000 00:00 0
7fefa85b2000-7fefa85b3000 r--p 00021000 fd:01 658372 /usr/lib64/ld-2.17.so
7fefa85b3000-7fefa85b4000 rw-p 00022000 fd:01 658372 /usr/lib64/ld-2.17.so
7fefa85b4000-7fefa85b5000 rw-p 00000000 00:00 0
7ffc62a81000-7ffc62aa2000 rw-p 00000000 00:00 0 [stack]
7ffc62aca000-7ffc62acc000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
赋值运算符重载
赋值运算符重载,即对=进行重载,使的运算符=可以针对自定义类型进行赋值操作。
注意事项
- 传参和传返回值用引用。
- 有返回值,支持连续赋值。
- 检测是否自己给自己赋值。
- 返回*this。
- 一个类如果没有显式定义赋值运算符重载函数,编译器也会生成一个,完成对象按字节序的值拷贝(浅拷贝)。同样的,如果类中有动态开辟的空间,需要自己手动实现一个赋值运算符重载函数,浅拷贝会出问题。
- 特别注意:Date d2 = d1;调用的是拷贝构造函数,而不是赋值运算符重载函数。因为这条语句完成的是初始化,而不是赋值。
五个不能重载的运算符
.* :: sizeof ?: .
运算符重载和函数重载没关系,不过运算符重载函数可以进行函数重载。
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void Display(){
cout << _year << "-" << _month << "-" << _day << endl;
}
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;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
Date d2;
d2.Display();
d2 = d1;
d2.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2018-12-31
2000-1-1
2018-12-31
Date d2 = d1;调用的是拷贝构造函数,而不是赋值运算符重载函数
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
Date(const Date& d){
cout << "Copy Constructor!" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d){
cout << "Opreator= !" << endl;
if(this != &d){
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Display(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
Date d2 = d1;
d2.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2018-12-31
Copy Constructor!
2018-12-31
const成员
const修饰类的成员函数
const修饰的类的成员函数称为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void Display(){
cout << "Member Func!" << endl;
cout << _year << "-" << _month << "-" << _day << endl;
}
void Display() const{
cout << "Const Member Func!" << endl;
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
const Date d2(2019, 5, 20);
d2.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
Member Func!
2018-12-31
Const Member Func!
2019-5-20
思考下面几个问题
const对象可以调非const成员函数吗?
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void Display(){
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
const Date d1(2018, 12, 31);
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
date.cc: In function ‘int main()’:
date.cc:27:13: error: passing ‘const Date’ as ‘this’ argument of ‘void Date::Display()’ discards qualifiers [-fpermissive]
d1.Display();
^
非const对象可以调const成员函数吗?
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void Display() const{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2018-12-31
const成员函数内可以调用其他非const成员函数吗?
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void test(){
cout << _year << "-" << _month << "-" << _day << endl;
}
void Display() const{
test();
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
date.cc: In member function ‘void Date::Display() const’:
date.cc:19:9: error: passing ‘const Date’ as ‘this’ argument of ‘void Date::test()’ discards qualifiers [-fpermissive]
test();
^
非const成员函数内可以调用其他const成员函数吗?
代码演示
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
void test() const{
cout << _year << "-" << _month << "-" << _day << endl;
}
void Display(){
test();
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1(2018, 12, 31);
d1.Display();
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
2018-12-31
总结
通过上述代码演示,可以看出。
- const对象不可以调用非const成员函数。
- 非const对象可以调用const成员函数。
- const成员函数内不可以调用其他的非const成员函数。
- 非const成员函数内可以调用其他const成员函数。
总结一下,解释const类型的不能去调用非const类型的。因为const类型权限小,非const类型权限大,const如果可以调用非const,相当于是对权限的放大。非const调用const,相当于是权限的缩小,这是允许的。
取地址及const取地址操作符重载
这两个默认成员函数一般不用定义,使用编译器默认生成的即可。
只有特殊情况才需要重载:比如,不想让获取到对象的地址。
代码演示
#include <iostream>
using std::cout;
using std::endl;
class Date{
public:
Date(int year = 2000, int month = 1, int day = 1){
_year = year;
_month = month;
_day = day;
}
Date* operator&(){
cout << "operator&" << endl;;
return this;
}
const Date* operator&() const{
cout << "const operator&" << endl;
return this;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d1;
const Date d2;
cout << "&d1: " << &d1 << endl;
cout << "&d2: " << &d2 << endl;
return 0;
}
运行结果
[sss@aliyun class_and_obj]$ !g++
g++ date.cc -o date
[sss@aliyun class_and_obj]$ ./date
operator&
&d1: 0x7ffca35e1a40
const operator&
&d2: 0x7ffca35e1a30
若不想让获取到对象地址
Date* operator&(){
return nullptr;
}
const Date* operator&() const{
return nullptr;
}
类中默认的成员函数只有两个吗?
C++11中加了两个,一共有八个。
新加的两个为:defaulted函数和deleted函数。