Итак, у меня есть целая программа ниже, которая создает объекты Book, инициализирует их и печатает любые конструкторы / деструкторы, которые были созданы или уничтожены в ходе выполнения программы.
Я запустил свой код (и вставил вывод ниже), и мне трудно понять, как называются деструкторы. Итак, я знаю, что конструкторы уничтожаются в том порядке, в котором они были созданы. Но я не понимаю, почему четыре из операторов-деструкторов имеют идентификатор 4. Я предполагаю, что одно пришло из «явного вызова конструктора копирования», а другое из «объявления и инициализации книги 6 из книги 5», а другой - из первой части «объявления и инициализации книг с 1 по 4». Но я не понимаю, откуда взялся дополнительный идентификатор 4?
Кроме того, мне было интересно, почему "- dtor: 0" не был напечатан для части "Объявление книги 5", где был создан ctor: 0 по умолчанию.
Я был бы очень признателен любое уточнение!
main. 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:
#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:
#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