目录
一般来说一个最简单的空类会有最基本的六大成员函数:构造函数、析构函数、拷贝构造函数、赋值运算符重载函数、取地址操作符重载、const修饰的取地址操作符重载.下面一一进行介绍.
一.构造函数
它是一个特殊的成员函数,不存在返回值,名字和类名相同,只有在实例化对象时才会进行自动调用.
#include<iostream>
using namespace std;
class Date
{
public:
// 1.无参构造函数
Date()
{}
// 2.带参构造函数
Date(int year, int month, int day)
{
m_year = year;
m_month = month;
m_day = day;
}
void print()
{
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
private:
int m_year;
int m_month;
int m_day;
};
void TestDate()
{
Date d1; // 调用无参构造函数
d1.print();//随机值
Date d2(2020, 1, 1); // 调用带参的构造函数
d2.print();//2020-1-1
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
//Date d3();//
}
int main()
{
TestDate();
return 0;
}
作用:
主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值
- 给创建的对象建立一个标识符;
- 为对象数据成员开辟内存空间;
- 完成对象数据成员的初始化。
特点:
- 函数名与类名相同,构造函数存在参数, 无返回值。
- 系统会自动提供一个默认的构造函数,如果自己实现了构造函数,则系统不再提供默认的构造函数。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载,它与其他的构造函数是以函数重载的方式共同存在的。
- 无参构造函数和全缺省构造函数都称为默认构造函数,并且默认构造函数只能有一个否则调用的时候就会产生冲突。无参
构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。 - 没有this指针。因为构造函数才是创建对象的,没有创建对象就不会有对象的首地址。
注意:
构造函数的主要任务并不是开空间创建对象,而是初始化对象。
构造函数初始化方法有两种:
- 赋值初始化:通过在函数体内进行赋值初始化。
- 列表初始化:在冒号后使用初始化列表进行初始化。
在我们使用初始化列表进行初始化时,要注意一个特性,它是关于C++初始化类成员的。它们是按照声明的顺序初始化的,而不是按照出现在初始化列表中的顺序。还有就是尽量使用初始化列表,因为它更高效。
#include<iostream>
using namespace std;
class Date
{
public:
// 1.无参构造函数
Date()
{
m_year = 2020;
m_month = 1;
m_day = 1;
}
//2.全缺省构造函数
/*Date(int year, int month, int day)
{
m_year = year;
m_month = month;
m_day = day;
}*/
//3.列表初始化,与2只能存其一
Date(int year, int month, int day) :
m_year(year),
m_month(month),
m_day(day)
{
}
void print()
{
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
private:
int m_year;
int m_month;
int m_day;
};
void TestDate()
{
Date d1; // 调用无参构造函数
d1.print();//2020-1-1
Date d2(2020, 1, 1); //调用全缺省构造函数
d2.print();//2020-1-1
}
int main()
{
TestDate();
system("pause");
return 0;
}
二.析构函数
与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。
特点:
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值。
- 一个类有且只有一个析构函数所以肯定不能重载。若未显式定义,系统会自动生成默认的析构函数。 对象生命周期结束时,C++编译系统系统自动调用析构函数。
- 析构函数是当一个栈被销毁前调用的,在C++中,当一个函数栈被销毁前,会调用栈中的每一个对象的析构函数.
- 对象生命周期结束后,后构造的对象先释放。
#include<iostream>
using namespace std;
class Date
{
public:
// 1.无参构造函数
Date()
{
m_year = 2020;
m_month = 1;
m_day = 1;
}
//2.全缺省构造函数
/*Date(int year, int month, int day)
{
m_year = year;
m_month = month;
m_day = day;
}*/
//3.列表初始化,与2只能存其一
Date(int year, int month, int day) :
m_year(year),
m_month(month),
m_day(day)
{
}
~Date()
{
cout << "调用析构函数" << endl;
}
void print()
{
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
private:
int m_year;
int m_month;
int m_day;
};
void TestDate()
{
Date d1; // 调用无参构造函数
d1.print();//2020-1-1
Date d2(2020, 1, 1); //调用全缺省构造函数
d2.print();//2020-1-1
}
int main()
{
TestDate();
system("pause");
return 0;
}
三.拷贝构造函数
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
特点:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
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;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);//等价于d2=d1;
return 0;
}
3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
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);
}
四.赋值运算符重载
定义方法
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
- 函数名字为:关键字operator后面接需要重载的运算符符号。
- 函数原型:返回值类型 operator操作符(参数列表)
特点:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型或者枚举类型的操作数
- 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
- 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
- . (成员访问运算符) . * (成员指针访问运算符) :: (作用域运算符) sizeof (长度运算符)? : (条件运算符)注意以上5个运算符不能重载
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// bool operator==(Date* this, const Date& d2)
// 这里需要注意的是,左操作数是this指向的调用函数的对象
bool operator==(const Date& d2)
{
return _year == d2._year
&& _month == d2._month
&& _day == d2._day;
}
private:
int _year;
int _month;
int _day;
};
void Test ()
{
Date d1(2020, 2, 26);
Date d2(2018, 9, 27);
cout<<(d1 == d2)<<endl;
}
以赋值运算符为例:
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;
}
}
private:
int _year ;
int _month ;
int _day ;
};
赋值运算符特点:
- 返回值
- 检测是否自己给自己赋值
- 返回*this
- 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
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;
Date d2(2018,10, 1);
// 这里d1调用的编译器生成operator=完成拷贝,d2和d1的值也是一样的。
d1 = d2;
return 0;
}
深拷贝:当成员中有指向堆的指针,就必须重新给该指针分配空间,然后将目标指针所指向的空间的内容拷贝到新分配的空间里.(如果不这样做,会导致两个指针指向同一块空间,从而导致在析构中出现多次释放).
五.const成员
定义:
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
#include<iostream>
using namespace std;
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();
}
int main()
{
Test();
system("pause");
return 0;
}
注意: 权限只能缩小不能被放大 const权限小(仅可读),非const权限大(可读可写)
关于const几个问题
1. const对象可以调用非const成员函数吗? 不可以;防止被修改.
2. 非const对象可以调用const成员函数吗? 可以
3. const成员函数内可以调用其它的非const成员函数吗?不可以;防止被修改.
4. 非const成员函数内可以调用其它的const成员函数吗?可以
六.取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
一般不需要写,系统会提供只有特殊情况,才需要重载,比如想让别人获取到指定的内容!