何時調用C ++析構函數? - When is a C++ destructor called?

問題:

Basic Question: when does a program call a class' destructor method in C++? 基本問題:程序何時在C ++中調用類的析構函數方法? I have been told that it is called whenever an object goes out of scope or is subjected to a delete 有人告訴我,只要一個對象超出範圍或被delete就會被調用

More specific questions: 更具體的問題:

1) If the object is created via a pointer and that pointer is later deleted or given a new address to point to, does the object that it was pointing to call its destructor (assuming nothing else is pointing to it)? 1)如果通過指針創建對象並且稍後刪除該指針或給出指向的新地址,那麼它所指向的對象是否會調用其析構函數(假設沒有其他指向它)?

2) Following up on question 1, what defines when an object goes out of scope (not regarding to when an object leaves a given {block}). 2)關注問題1,什麼定義了一個對象何時超出範圍(不是關於對象何時離開給定的{block})。 So, in other words, when is a destructor called on an object in a linked list? 那麼,換句話說,什麼時候是一個析構函數調用鏈表中的對象?

3) Would you ever want to call a destructor manually? 3)你想要手動調用析構函數嗎?


解決方案:

參考: https://stackoom.com/en/question/gIdh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章