简体   繁体   English

C++ - 阐明何时以及如何调用析构函数

[英]C++ - Clarifying when and how destructors are called

So I have an entire program below that creates Book objects, initializes them, and prints any constructors/destructors that are created or destroyed throughout the execution of the program.所以我在下面有一个完整的程序,它创建 Book 对象,初始化它们,并打印在整个程序执行过程中创建或销毁的任何构造函数/析构函数。

I have ran my code (and pasted the output below), and I am having trouble understanding how the destructors are being called.我已经运行了我的代码(并粘贴了下面的输出),但我无法理解如何调用析构函数。 So I know that the constructors are destroyed in the opposite order in which they were created.所以我知道构造函数的销毁顺序与它们的创建顺序相反。 But I don't get why four of the destructor statements have an id of 4. I'm assuming one came from the "explicit call to the copy constructor", the other came from "declaring and initializing book 6 from book 5", and the other came from the first part of "declaring and initializing books 1 to 4."但我不明白为什么四个析构函数语句的 id 为 4。我假设一个来自“对复制构造函数的显式调用”,另一个来自“从第 5 册中声明和初始化第 6 册”,另一个来自“声明和初始化书籍1到4”的第一部分。 But I'm confused as to where the extra id of 4 came from?但我很困惑 4 的额外 id 来自哪里?

Additionally, I was wondering why a "-- dtor: 0" wasn't printed for the "declaring book 5" part where default ctor: 0 was created.此外,我想知道为什么没有为创建默认 ctor: 0 的“声明书 5”部分打印“-- dtor:0”。

I would really appreciate any clarification!我真的很感激任何澄清!

main.cc:主.cc:

#include <iostream>
#include <string>
using namespace std;

#include "Book.h"


void func1(Book);
void func2(Book&);


int main()
{
  cout<<endl<<"Declaring and initializing books 1 to 4..."<<endl;

  Book b1(1, "Ender's Game", "Orson Scott Card");
  Book b2(2, "Dune", "Frank Herbert");
  Book b3(3, "Foundation", "Isaac Asimov");
  Book b4(4, "Hitch Hiker's Guide to the Galaxy", "Douglas Adams");

  cout<<endl<<"Declaring book 5..."<<endl;
  Book b5;
  b5.print();

  cout<<endl<<"Assigning book 4 to 5..."<<endl;
  b5 = b4;
  b5.print();

  cout<<endl<<"Declaring and initializing book 6 from book 5..."<<endl;
  Book b6 = b5;
  b6.print(); 

  cout<<endl<<"Calling func1()..."<<endl;
  func1(b1);

  cout<<endl<<"Calling func2()..."<<endl;
  func2(b2);

  cout<<endl<<"Explicit call to copy constructor..."<<endl;
  Book b7(b6);


  cout << endl << endl;

  return 0;
}

void func1(Book b)
{
  b.print();
}

void func2(Book& b)
{
  b.print();
}

book.cc:书.cc:

#include <iostream>
#include <string>
using namespace std;
#include "Book.h"

Book::Book(int i, string t, string a)
{
  cout<<"-- default ctor:  "<< i <<endl;
  id     = i;
  title  = t;
  author = a;
}

Book::Book(const Book& other)
{
  id     = other.id;
  title  = other.title;
  author = other.author;

  cout<<"-- copy ctor:  "<< id <<endl;
}


Book::~Book()
{
  cout<<"-- dtor:  "<< id <<endl;
}

void Book::print()
{
  cout<<"** "<< title <<" by "<<author<<endl;
}

book.h:书.h:

#ifndef BOOK_H
#define BOOK_H

#include <string>
using namespace std;

class Book
{
  public:
    Book(int=0, string="Unknown", string="Unknown");
    Book(const Book&);
    ~Book();
    void print();

  private:
    int id;
    string title;
    string author;
};

#endif

OUTPUT:输出:

Declaring and initializing books 1 to 4...
-- default ctor:  1
-- default ctor:  2
-- default ctor:  3
-- default ctor:  4

Declaring book 5...
-- default ctor:  0
** Unknown by Unknown

Assigning book 4 to 5...
** Hitch Hiker's Guide to the Galaxy by Douglas Adams

Declaring and initializing book 6 from book 5...
-- copy ctor:  4
** Hitch Hiker's Guide to the Galaxy by Douglas Adams

Calling func1()...
-- copy ctor:  1
** Ender's Game by Orson Scott Card
-- dtor:  1

Calling func2()...
** Dune by Frank Herbert

Explicit call to copy constructor...
-- copy ctor:  4


-- dtor:  4
-- dtor:  4
-- dtor:  4
-- dtor:  4
-- dtor:  3
-- dtor:  2
-- dtor:  1

I don't get why four of the destructor statements have an id of 4我不明白为什么四个析构函数语句的 id 为 4

because you assign b4 to b5 in statement因为您在语句中将b4分配给b5

b5 = b4;

then copy construct b6 = b5;然后复制构造b6 = b5; and b7(b6);b7(b6); each of them having id = 4, so destructor prints他们每个人都有id = 4,所以析构函数打印

-- dtor: 4

Additionally, I was wondering why a "-- dtor: 0" wasn't printed for the "declaring book 5" part where default ctor: 0 was created.此外,我想知道为什么没有为创建默认 ctor: 0 的“声明书 5”部分打印“-- dtor:0”。

because when Book b5;因为当Book b5; is created it had id = 0 but when code assigned b4 to b5 id became 4, hence no "-- dtor: 0" was printed.创建它时,它的id = 0 但是当代码将b4分配给b5 id变为 4 时,因此没有打印“-- dtor:0”。

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

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