构造函数
构造函数在对象创建的时候自动调用,一般用于完成对象的初始化工作。
特点:
- 函数名与类同名,没有返回值,void都不能写。可以有参数、可以重载、可以有多个构造函数。
- 一旦自定义了构造函数,就必须用其中一个自定义的构造函数来初始化对象。
- 需要声明为public,外界才能访问。
通过malloc分配的对象不会调用构造函数。
构造函数的调用
C++提供了两种方式使用构造函数初始化对象。
第一种显示的调用构造函数
class Cat{public:int id;int age;int weight;Cat(int i,int a,int w){id = i;age = a;weight = w;}};
显示的调用构造函数:
Cat cat1 = Cat(1,18,70);
第二种隐式的调用构造函数:
Cat cat1(1,18,70);//这种方式调用格式上更紧凑,与上面显示调用是等价的。
也可以使用指针来管理对象,配合new动态分配内存,构造函数与new一起使用如下:
Cat *cat3 = new Cat(1,18,70);//调用构造函数
关于成员名称和构造函数参数名称
不要将类成员的名称用作构造函数的参数名称。例如:
class Cat{public:int id;int age;int weight;Cat(int id,int age,int weight){ //错误的写法id = id;age = age;weight = weight;}};int main(int argc, const char * argv[]) {Cat *cat3 = new Cat(1,18,70);return 0;}
构造函数的参数表示的不是类成员,而是赋给类成员的值。因此,参数名不能与类成员相同,否则构造函数的参数不能赋值给创建对象的成员。
为了避免这种情况,业界通常有两种处理方式:
在类成员名称前加m_前缀,比如
class Cat{public:int m_id;int m_age;int m_weight;Cat(int id,int age,int weight){m_id = id;m_age = age;m_weight = weight;}};
在成员名称后面加_
class Cat{public:int id_;int age_;int weight_;Cat(int id,int age,int weight){id_ = id;age_ = age;weight_ = weight;}};
无论采取哪一种方法都是可行的,可以根据个人喜好和公司团队规范使用。后续代码中我们使用m_前缀的方式来命名成员变量名称。
析构函数
析构函数(析构器),在对象销毁的时候自动调用,一般用于完成对象的清理工作。
特点:
函数名以~开头,与类同名,无返回值(void都不能写),无参,不可以重载,有且只有一个析构函数
注意事项
- 构造函数、析构函数需要声明public,外界才能访问。
- 通过malloc创建的对象,free的时候不会调用析构函数。
class Cat{public:int m_id;int m_age;int m_weight;Cat(int id,int age,int weight){m_id = id;m_age = age;m_weight = weight;}~Cat(){ //析构函数}};
<br /> 例如,如果构造函数使用new来分配内存,则析构函数将使用delete来释放这些内存。
#include <iostream>class Cat{public:int m_age;Cat(int age){std::cout << "Cat 的构造函数被调用" << std::endl;m_age = age;}~Cat(){ //析构函数std::cout << "~Cat 的析构函数被调用" << std::endl;}};class Person{public:int m_height;Cat * m_cat;Person(int height,Cat * cat){std::cout << "Person 的构造函数被调用" << std::endl;m_height = height;m_cat = cat;}~Person(){ //使用delete来释放内存。delete m_cat;std::cout << "~Person 的析构函数被调用" << std::endl;}};int main(int argc, const char * argv[]) {{Person *person = new Person(100,new Cat(18));delete person;}return 0;}
内存布局如下图:
为了更好的理解当创建对象时,内存分布情况,我们看以下3种情况。
第一种:
#include <iostream>class Cat{public:int m_age;Cat(int age){std::cout << "Cat 的构造函数被调用" << std::endl;m_age = age;}};class Person{public:int m_height;Cat * m_cat;Person(int height,Cat * cat){std::cout << "Person 的构造函数被调用" << std::endl;m_height = height;m_cat = cat;}~Person(){delete m_cat;std::cout << "~Person 的析构函数被调用" << std::endl;}};int main(int argc, const char * argv[]) {{Person person(100,new Cat(18)); //隐私构造,创建的person对象在栈空间中}return 0;}
在栈空间中创建了person对象,内存分布如下图所示:
第二种:
#include <iostream>class Cat{public:int m_age;Cat(int age){std::cout << "Cat 的构造函数被调用" << std::endl;m_age = age;}};class Person{public:int m_height = 100;Cat m_cat = Cat(18);};int main(int argc, const char * argv[]) {{Person person;}return 0;}
内存布局如下:
第三种情况:
#include <iostream>class Cat{public:int m_age;Cat(int age){std::cout << "Cat 的构造函数被调用" << std::endl;m_age = age;}};class Person{public:int m_height = 100;Cat m_cat = Cat(18);};int main(int argc, const char * argv[]) {Person * person = new Person();return 0;}

以上基本涵盖开发中的所有情况。
