簡體   English   中英

我需要在此類的析構函數中編寫任何內容嗎?

[英]Do I need to write anything in the destructor of this class?

謝謝大家! 現在我改變了邏輯。 因為如果我包含指向自身的相同指針,它將創建無限循環。 因此,對於此修訂版,我需要編寫析構函數嗎?

#include <stdio.h>
#include <stdlib.h>
#include <tr1/array>

using namespace std;
class Graphnode {

public:
    std::tr1::array<int, 16> state;
    int x;
    int depth;
    Graphnode(std::tr1::array<int, 16>,int,int);
    Graphnode();
    //~Graphnode();

};
Graphnode::Graphnode()
{
    int i=0;
    for(i=0;i<16;i++)
    {
       state[i] = 0;
    }
    x = 0;
    depth = 0;
}
Graphnode::Graphnode(std::tr1::array<int, 16> _state,int _x,int _d)
{   
    int i=0;
    for(i=0;i<16;i++)
    {
       state[i] = _state[i];
    }
    x = _x;
    depth = _d;
}
/*Graphnode::~Graphnode()
{
}*/

您沒有在堆上分配任何東西,因此在對象被銷毀時不需要清理任何東西。 編譯器會自動為您生成一個析構函數,並且IIRC會執行與您注釋掉的空對象基本上相同的操作,盡管有一些明顯的不同。 關於空析構函數和編譯器生成的析構函數之間的區別, 這是一個很好的問題/答案。

當您嘗試創建一個Graphnode時,您的代碼將創建一個無限循環

您可以嘗試為子Graphnode創建一個特定的構造函數,以便在該構造函數中不創建更多的Graphnode。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM