简体   繁体   English

为什么构造函数和析构函数调用存在差异?

[英]Why there is difference in constructor and destructor calls?

Header file: Date.h file Header 文件: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;
    }

  };

Header file: Employee.h Header 文件: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() function :主要() function

#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!一旦执行,我们得到这个!

So, the question is as we can see there are 4 date constructors are being executed and then there is class Employee constructor is being called.所以,问题是我们可以看到有 4 个日期构造函数正在执行,然后是 class Employee 构造函数正在被调用。 Next, two date destructors are being executed.接下来,正在执行两个日期析构函数。 Now, when we get "press an key" option and once we press, we get a call of Employee destructor before 4 more date destructor calls.现在,当我们获得“按下一个键”选项并且一旦按下,我们就会在另外 4 个日期析构函数调用之前调用 Employee 析构函数。 Thus, there are total 4 date constructor calls while 6 date destructor calls.因此,总共有 4 个日期构造函数调用,而 6 个日期析构函数调用。 However, class Employee calls one constructor and destructor.但是,class Employee 调用了一个构造函数和析构函数。

**Why there is 4 date constructor calls and more destructor calls, 6 destructor calls? **为什么有 4 个日期构造函数调用和更多的析构函数调用,6 个析构函数调用? Further, can anybody elaborate the sequence and specify the points at where these constructor and destructor are being called, one by one.此外,任何人都可以详细说明序列并一一指定调用这些构造函数和析构函数的点。

Further, point to be noted, member objects are being passed to Employee constructor by value.此外,需要注意的是,成员对象是按值传递给 Employee 构造函数的。 But if we pass by reference, then there are 4 date constructor calls and 4 date destructor calls while one and one constructor and destructor calls of class Employee.但是,如果我们通过引用传递,那么有 4 个日期构造函数调用和 4 个日期析构函数调用,而 class Employee 的构造函数和析构函数调用分别是一个和一个。 Check the picture.检查图片。

constructor and destructor calls when reference of objects is being passed to class Employee constructor将对象的引用传递给 class Employee 构造函数时调用构造函数和析构函数

I'm a newbie, so it would be a great help.我是新手,所以这将是一个很大的帮助。 Thanks **谢谢 **

The two additional destructor calls are generated by the Date objects generated by the implicit copy constructors when passed by value to the Employee class constructor.当通过值传递给Employee class 构造函数时,隐式复制构造函数生成的Date对象会生成另外两个析构函数调用。 These two new Date objects are destroyed as soon as the Employee constructor finishes.一旦Employee构造函数完成,这两个新的Date对象就会被销毁。

When you pass by value, you are in fact creating another object for the function's lifetime.当您按值传递时,您实际上是在为函数的生命周期创建另一个 object。

Notice how you Pass by value two Data objects in Employee's Constructor?请注意您如何在 Employee 的构造函数中传递两个 Data 对象的值? That means that two new Date objects are created up until Employee's constructor returns.这意味着在 Employee 的构造函数返回之前,将创建两个新的 Date 对象。

These values are called automatic, and once constructor exits they are automatically deleted,as a result their destructor's are called.这些值被称为自动的,一旦构造函数退出,它们就会被自动删除,因此它们的析构函数被调用。 This is where your two extra destructor calls come from.这是您的两个额外的析构函数调用的来源。

Edit: You create 2 Date objects, so in fact 2 Date constructor calls编辑:您创建 2 个 Date 对象,所以实际上 2 Date 构造函数调用

You then create an employee object, so you call employee's constructor and also another 2 Date object's constructors(since each employee object has 2 Date members )然后创建一个员工 object,因此您调用员工的构造函数以及另外 2 个日期对象的构造函数(因为每个员工 object 有 2 个日期成员)

Since employee's constructor is called, you have the 2 constructor calls i mentioned above, so total 6 Date construction calls, now once your employee's constructor ends, the Date object's that were created by "call by value" are deleted and thus their destructors are called, so total 2 destructors so far,由于调用了员工的构造函数,你有上面提到的 2 个构造函数调用,所以总共 6 个 Date 构造调用,现在一旦你的员工的构造函数结束,由“按值调用”创建的 Date 对象被删除,因此它们的析构函数被调用,到目前为止总共有 2 个析构函数,

Once Employee object gets deleted,its 2 date objects get as well,so that's another 2 destructor calls.一旦 Employee object 被删除,它的 2 个日期对象也会被删除,所以这是另外 2 个析构函数调用。

Then another 2 Date objects are deleted( declared in main ) and thus another 2 destructor calls.然后另外 2 个 Date 对象被删除(在 main 中声明),因此另外 2 个析构函数调用。

Good luck learning C++!祝你学习 C++ 好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM