【问题标题】:Why there is difference in constructor and destructor calls?为什么构造函数和析构函数调用存在差异?
【发布时间】:2020-08-24 23:46:39
【问题描述】:

头文件:Date.h 文件

#include<iostream>
#include<cstdlib>   
#include<cstring>  

using namespace std;

class Date{

private:
    int day;
    int month;
    int year;

public:
    Date(int d = 1, int m = 1, int y = 1900): day(d), month(m), year(y) 
    {
        cout << "date constructor is called"<< endl;
    }

    void print() const {
        cout << day << ":" << month << ":" << year <<endl;
    }

    ~Date(){
        cout << "date destructor is called"<< endl;
    }

  };

头文件:Employee.h

#include"Date.h"

class Employee{

private:
    char *fname;
    char *lname;
    Date dob;       // object, has-a relationship
    Date hiredate;  // object, has-a relationship

public:
    Employee(char *f, char *l, Date bd, Date hd){ 
        cout << "employee constructor is called"<< endl;
        int lengthf;
        lengthf = strlen(f);
        fname = new char[lengthf+1];  
        strcpy(fname, f);

        int lengthl;
        lengthl = strlen(l);
        lname = new char[lengthl +1];
        strcpy(lname, l);

    }

    ~Employee(){
        delete [] fname;  
        delete [] lname;
        cout << "employee destructor is called"<< endl;
    }


   };

main() 函数

#include"Employee.h"

int main(){

Date db(07, 11, 1991);
Date dh; 

dh.print();

Employee e("Dan", "Lee", db , dh);  


db.print();



system("pause");
return 0;
}

Once executed, we get this!

所以,问题是我们可以看到有 4 个日期构造函数正在执行,然后有 Employee 类构造函数被调用。接下来,正在执行两个日期析构函数。现在,当我们获得“按下一个键”选项并且一旦按下,我们就会在另外 4 个日期析构函数调用之前调用 Employee 析构函数。因此,总共有 4 个日期构造函数调用,而 6 个日期析构函数调用。但是,Employee 类调用了一个构造函数和析构函数。

**为什么有 4 个日期构造函数调用和更多的析构函数调用,6 个析构函数调用?此外,任何人都可以详细说明序列并一一指定调用这些构造函数和析构函数的点。

此外,需要注意的是,成员对象是按值传递给 Employee 构造函数的。但是如果我们通过引用传递,那么就会有 4 个日期构造函数调用和 4 个日期析构函数调用,而 Employee 类的构造函数和析构函数调用分别是一个和一个。检查图片。

constructor and destructor calls when reference of objects is being passed to class Employee constructor

我是一个新手,所以这将是一个很大的帮助。谢谢**

【问题讨论】:

  • 请将输出作为文本而不是图像复制到问题中。

标签: c++ constructor destructor copy-constructor


【解决方案1】:

两个额外的析构函数调用是由隐式复制构造函数生成的Date 对象生成的,这些对象是通过值传递给Employee 类构造函数时生成的。一旦Employee 构造函数完成,这两个新的Date 对象就会被销毁。

【讨论】:

  • 我已经考虑过您的回答,先生。我想我无法得到您的评论 :) 我已经考虑并接受了您的回答。不是? ?
  • @RizwanMuzaffar “接受答案”在 Stack Overflow 上有其自身的含义。参考this description了解更多?
【解决方案2】:

当你通过值传递时,你实际上是在为函数的生命周期创建另一个对象。

注意您如何在 Employee 的构造函数中传递两个 Data 对象的值?这意味着在 Employee 的构造函数返回之前,会创建两个新的 Date 对象。

这些值被称为自动的,一旦构造函数退出它们就会被自动删除,因此它们的析构函数被调用。这就是你的两个额外的析构函数调用的来源。

编辑: 您创建了 2 个 Date 对象,因此实际上调用了 2 个 Date 构造函数

然后您创建一个员工对象,因此您调用员工的构造函数以及另外 2 个日期对象的构造函数(因为每个员工对象都有 2 个日期成员)

由于调用了员工的构造函数,因此您有上面提到的 2 个构造函数调用,所以总共 6 个 Date 构造调用,现在一旦您的员工的构造函数结束,由“按值调用”创建的 Date 对象将被删除,因此它们的析构函数被调用,所以到目前为止总共有 2 个析构函数,

一旦 Employee 对象被删除,它的 2 个日期对象也会被删除,所以这是另外 2 个析构函数调用。

然后另外 2 个 Date 对象被删除(在 main 中声明),因此另外 2 个析构函数调用。

祝你学习 C++ 好运!

【讨论】:

  • 谢谢,真的很有帮助?
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 2015-04-28
  • 2011-04-16
  • 1970-01-01
  • 2019-11-16
  • 2014-04-22
  • 2021-07-28
  • 1970-01-01
相关资源
最近更新 更多