构造函数

构造函数在对象创建的时候自动调用,一般用于完成对象的初始化工作。

特点:

  • 函数名与类同名,没有返回值,void都不能写。可以有参数、可以重载、可以有多个构造函数。
  • 一旦自定义了构造函数,就必须用其中一个自定义的构造函数来初始化对象。
  • 需要声明为public,外界才能访问。

通过malloc分配的对象不会调用构造函数。

构造函数的调用

C++提供了两种方式使用构造函数初始化对象。
第一种显示的调用构造函数

  1. class Cat{
  2. public:
  3. int id;
  4. int age;
  5. int weight;
  6. Cat(int i,int a,int w){
  7. id = i;
  8. age = a;
  9. weight = w;
  10. }
  11. };

显示的调用构造函数:

  1. Cat cat1 = Cat(1,18,70);

第二种隐式的调用构造函数:

  1. Cat cat1(1,18,70);//这种方式调用格式上更紧凑,与上面显示调用是等价的。

也可以使用指针来管理对象,配合new动态分配内存,构造函数与new一起使用如下:

  1. Cat *cat3 = new Cat(1,18,70);//调用构造函数

关于成员名称和构造函数参数名称
不要将类成员的名称用作构造函数的参数名称。例如:

  1. class Cat{
  2. public:
  3. int id;
  4. int age;
  5. int weight;
  6. Cat(int id,int age,int weight){ //错误的写法
  7. id = id;
  8. age = age;
  9. weight = weight;
  10. }
  11. };
  12. int main(int argc, const char * argv[]) {
  13. Cat *cat3 = new Cat(1,18,70);
  14. return 0;
  15. }

构造函数的参数表示的不是类成员,而是赋给类成员的值。因此,参数名不能与类成员相同,否则构造函数的参数不能赋值给创建对象的成员。
为了避免这种情况,业界通常有两种处理方式:

  • 在类成员名称前加m_前缀,比如

    1. class Cat{
    2. public:
    3. int m_id;
    4. int m_age;
    5. int m_weight;
    6. Cat(int id,int age,int weight){
    7. m_id = id;
    8. m_age = age;
    9. m_weight = weight;
    10. }
    11. };
  • 在成员名称后面加_

    1. class Cat{
    2. public:
    3. int id_;
    4. int age_;
    5. int weight_;
    6. Cat(int id,int age,int weight){
    7. id_ = id;
    8. age_ = age;
    9. weight_ = weight;
    10. }
    11. };

    无论采取哪一种方法都是可行的,可以根据个人喜好和公司团队规范使用。后续代码中我们使用m_前缀的方式来命名成员变量名称。

析构函数

析构函数(析构器),在对象销毁的时候自动调用,一般用于完成对象的清理工作。
特点:
函数名以~开头,与类同名,无返回值(void都不能写),无参,不可以重载,有且只有一个析构函数

注意事项

  • 构造函数、析构函数需要声明public,外界才能访问。
  • 通过malloc创建的对象,free的时候不会调用析构函数。
  1. class Cat{
  2. public:
  3. int m_id;
  4. int m_age;
  5. int m_weight;
  6. Cat(int id,int age,int weight){
  7. m_id = id;
  8. m_age = age;
  9. m_weight = weight;
  10. }
  11. ~Cat(){ //析构函数
  12. }
  13. };
  1. <br /> 例如,如果构造函数使用new来分配内存,则析构函数将使用delete来释放这些内存。
  1. #include <iostream>
  2. class Cat{
  3. public:
  4. int m_age;
  5. Cat(int age){
  6. std::cout << "Cat 的构造函数被调用" << std::endl;
  7. m_age = age;
  8. }
  9. ~Cat(){ //析构函数
  10. std::cout << "~Cat 的析构函数被调用" << std::endl;
  11. }
  12. };
  13. class Person{
  14. public:
  15. int m_height;
  16. Cat * m_cat;
  17. Person(int height,Cat * cat){
  18. std::cout << "Person 的构造函数被调用" << std::endl;
  19. m_height = height;
  20. m_cat = cat;
  21. }
  22. ~Person(){ //使用delete来释放内存。
  23. delete m_cat;
  24. std::cout << "~Person 的析构函数被调用" << std::endl;
  25. }
  26. };
  27. int main(int argc, const char * argv[]) {
  28. {
  29. Person *person = new Person(100,new Cat(18));
  30. delete person;
  31. }
  32. return 0;
  33. }

内存布局如下图:
Snipaste_2021-07-27_18-32-48.png
为了更好的理解当创建对象时,内存分布情况,我们看以下3种情况。

第一种:

  1. #include <iostream>
  2. class Cat{
  3. public:
  4. int m_age;
  5. Cat(int age){
  6. std::cout << "Cat 的构造函数被调用" << std::endl;
  7. m_age = age;
  8. }
  9. };
  10. class Person{
  11. public:
  12. int m_height;
  13. Cat * m_cat;
  14. Person(int height,Cat * cat){
  15. std::cout << "Person 的构造函数被调用" << std::endl;
  16. m_height = height;
  17. m_cat = cat;
  18. }
  19. ~Person(){
  20. delete m_cat;
  21. std::cout << "~Person 的析构函数被调用" << std::endl;
  22. }
  23. };
  24. int main(int argc, const char * argv[]) {
  25. {
  26. Person person(100,new Cat(18)); //隐私构造,创建的person对象在栈空间中
  27. }
  28. return 0;
  29. }

在栈空间中创建了person对象,内存分布如下图所示:
Snipaste_2021-07-27_18-46-12.png
第二种:

  1. #include <iostream>
  2. class Cat{
  3. public:
  4. int m_age;
  5. Cat(int age){
  6. std::cout << "Cat 的构造函数被调用" << std::endl;
  7. m_age = age;
  8. }
  9. };
  10. class Person{
  11. public:
  12. int m_height = 100;
  13. Cat m_cat = Cat(18);
  14. };
  15. int main(int argc, const char * argv[]) {
  16. {
  17. Person person;
  18. }
  19. return 0;
  20. }

内存布局如下:
Snipaste_2021-07-27_19-02-47.png

第三种情况:

  1. #include <iostream>
  2. class Cat{
  3. public:
  4. int m_age;
  5. Cat(int age){
  6. std::cout << "Cat 的构造函数被调用" << std::endl;
  7. m_age = age;
  8. }
  9. };
  10. class Person{
  11. public:
  12. int m_height = 100;
  13. Cat m_cat = Cat(18);
  14. };
  15. int main(int argc, const char * argv[]) {
  16. Person * person = new Person();
  17. return 0;
  18. }

Snipaste_2021-07-27_19-13-58.png
以上基本涵盖开发中的所有情况。