Hàm hủy trong C++

Tìm hiểu về phương thức khởi tạo (Constructor) và phương thức hủy (Destructor), cách sử dụng hiệu quả thông qua các ví dụ

Constructor và Destructor là gì?

Phương thức khởi tạo (Constructor) hay phương thức hủy (Destructor) là 2 trong số các phương thức mang lại sự tiện lợi khi lập trình với

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
5

  • Hàm tạo. tự động phương thức được gọi sau khi các đối tượng được tạo xong
  • Kẻ hủy diệt. tự động phương thức được gọi trước khi hủy đối tượng tiến trình

Cách sử dụng Constructor và Destructor trong C++

  • Hàm tạo. function is dedefine by way to set trùng lặp tên với tên
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    5 và không có kiểu trả về
  • Kẻ hủy diệt. được định nghĩa bằng cách đặt trùng lặp với tên
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    5 và thêm ký tự
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    8 vào hàm phía trước
#include 

class Node
{
public:
	// Constructor
	Node()
	{
		std::cout << "Constructor Called";
	}
	
	// Destructor
	~Node()
	{
		std::cout << "Destructor Called";
	}
}

int main()
{
	Node node;

	return 0;
}

Sau dòng

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
9

  • Đối tượng
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    0 được tạo ra
  • Sau đó
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    0 tự động được gọi
  • Đến giai đoạn kết thúc hàm
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    1, đối tượng
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    0 sẽ được thu hồi nhưng trước đó sẽ tự động gọi
    #include 
    
    class Node
    {
    private:
        Node *next;
    public:
    	void init()
    	{
    		this->next = nullptr;
    	}
    }
    
    int main()
    {
    	Node node;
    	node.init();
    
    	return 0;
    }
    3

Các trường hợp sử dụng Constructor tăng hiệu quả tính toán

Constructor và Destructor không bắt buộc phải định nghĩa. Trong ngôn ngữ những cảnh không cần thiết thì không cần định nghĩa 2 phương thức này

Câu chuyện của Constructor

Giả sử

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
4 là 1 phần tử trong danh sách liên kết, nghĩa là
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
5 sẽ lưu giữ vị trí của
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 kế tiếp hoặc đánh dấu là
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 cuối cùng (
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
8)

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}

Dòng

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
9 được gọi sau khi
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 được tạo ra, để chắc chắn rằng
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
5 luôn là
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 cuối cùng (khi chưa được thêm vào danh sách)

Điều này bất tiện vì

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
9 không đáng tin vì có lúc quên gọi, mặc dù điều này là 1 mong muốn mặc định khi 1
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 ra đời thì
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
5 phải là
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
26. Trong trường hợp này, nhà xây dựng sẽ thể hiện được sức mạnh của nó

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
2

Như vậy không cần phải luôn tự nhắc bản thân chỉ định

#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
8 sau khi
#include 

class Node
{
private:
    Node *next;
public:
	void init()
	{
		this->next = nullptr;
	}
}

int main()
{
	Node node;
	node.init();

	return 0;
}
0 được tạo ra

Câu chuyện của Destructor

Với ví dụ về Node trong danh sách liên kết, thấy được tính hiệu quả của Constructor, nhưng về Destructor không mang nhiều tính chất hữu ích rõ ràng, vì nút trong thực tế có thể được tái sử dụng, bị hủy sử dụng ngay lập tức, bị