前言
C++的类构成中会有构造函数和析构函数。
构造函数又分为默认构造、自定义构造、拷贝构造、自定义拷贝构造、移动构造(C11新特性)。
构造函数
1、构造函数必须与类同名。
2、C++允许构造函数重载。
3、构造函数没有返回值。
当对象被创建时,自动调用构造函数。
C++对于类都会自动生成“默认构造函数”和“默认拷贝构造函数”。
默认构造函数是不带参数的。
默认构造函数的作用是,给类中的变量分配内存空间。
对于类,编译器都会默认生成一个“不带参数的构造函数”,形式如下:
class CStructure
{
public:
CStructure()
{
std::cout << "默认构造函数" << std::endl;
}
};
默认拷贝构造函数是 只有一个参数并且参数是该类对象的引用的。
默认拷贝构造函数仅仅做了类成员内存空间分配和简单的值拷贝,即:浅拷贝。
对于类,编译器会默认生成一个“只有一个参数并且参数是该类对象的引用的 构造函数”,形式如下:
class CStructure
{
public:
CStructure(const CStructure &Other)
{
std::cout << "默认拷贝构造函数" << std::endl;
}
};
自定义构造函数是带参数的。
对于类,初始化列表进行对象的构造,形式如下:
class CStructure
{
public:
CStructure(const std::string &szName, int nAge, char *pBuff):m_szName(szName), m_nAge(nAge), m_pBuff(pBuff)
{
std::cout << "构造函数" << std::endl;
}
private:
std::string m_szName;
int m_nAge;
char *m_pBuff;
};
自定义拷贝构造函数。
对于类,自定义拷贝构造一般为深拷贝,形式如下:
class CStructure
{
public:
CStructure(CStructure &Other):m_szName(Other.m_szName), m_nAge(Other.m_nAge), m_pBuff(nullptr) // 指针初始化
{
if (Other.m_pBuff)
{
int nLen = strlen(Other.m_pBuff);
m_pBuff = new char[nLen + 1];
if (m_pBuff)
{
memcpy(m_pBuff, Other.m_pBuff, (nLen + 1));
}
}
std::cout << "深拷贝构造函数" << std::endl;
}
private:
std::string m_szName;
int m_nAge;
char *m_pBuff;
};
移动构造函数(C11新特性)
所谓移动语义,指的就是以移动而非深拷贝的方式初始化含有指针成员的类对象。简单的理解,移动语义指的就是将其他对象(通常是临时对象)拥有的内存资源“移为已用”。
非 const 右值引用只能操作右值,程序执行结果中产生的临时对象(例如函数返回值、lambda 表达式等)既无名称也无法获取其存储地址,所以属于右值。当类中同时包含拷贝构造函数和移动构造函数时,如果使用临时对象初始化当前类的对象,编译器会优先调用移动构造函数来完成此操作。只有当类中没有合适的移动构造函数时,编译器才会退而求其次,调用拷贝构造函数。
默认情况下,左值初始化同类对象只能通过拷贝构造函数完成,如果想调用移动构造函数,则必须使用右值进行初始化。C++11 标准中为了满足用户使用左值初始化同类对象时也通过移动构造函数完成的需求,新引入了 std::move() 函数,它可以将左值强制转换成对应的右值,由此便可以使用移动构造函数。
形式如下:
class CStructure
{
public:
CStructure(CStructure &&Other):m_szName(std::move(Other.m_szName)), m_nAge(Other.m_nAge), m_pBuff(Other.m_pBuff)
{
if (Other.m_pBuff)
{
Other.m_pBuff = nullptr; // 移动构造的关键
}
std::cout << "移动构造函数" << std::endl;
}
private:
std::string m_szName;
int m_nAge;
char *m_pBuff;
};
析构函数
1、构造函数必须与类同名。
2、构造函数没有返回值。
当对象被销毁时,自动调用析构函数,主要是资源的释放。
析构函数也分为默认析构函数和自定义析构函数。形式如下:
class CStructure
{
public:
~CStructure()
{
std::cout << "析构函数" << std::endl;
}
};
完整代码
#include <iostream>
class CStructure
{
public:
CStructure()
{
std::cout << "默认构造函数" << std::endl;
}
CStructure(const std::string &szName, int nAge, char *pBuff):m_szName(szName), m_nAge(nAge), m_pBuff(pBuff)
{
std::cout << "构造函数" << std::endl;
}
~CStructure()
{
std::cout << "析构函数" << std::endl;
}
CStructure(CStructure &Other):m_szName(Other.m_szName), m_nAge(Other.m_nAge), m_pBuff(nullptr)
{
if (Other.m_pBuff)
{
int nLen = strlen(Other.m_pBuff);
m_pBuff = new char[nLen + 1];
if (m_pBuff)
{
memcpy(m_pBuff, Other.m_pBuff, (nLen + 1));
}
}
std::cout << "深拷贝构造函数" << std::endl;
}
CStructure(CStructure &&Other):m_szName(std::move(Other.m_szName)), m_nAge(Other.m_nAge), m_pBuff(Other.m_pBuff)
{
if (Other.m_pBuff)
{
Other.m_pBuff = nullptr;
}
std::cout << "移动构造函数" << std::endl;
}
private:
std::string m_szName;
int m_nAge;
char *m_pBuff;
};
void test()
{
CStructure StructureA("C++", 20, "历久不衰");
CStructure StructureB(StructureA);
CStructure StructureC(std::move(StructureA));
}
int main(int argc, char* argv[])
{
test();
system("pause");
return 0;
}