2015-12-19 78 views
0

我有兩個構造函數,有一個字節*成員如下結構:如何在另一個結構中使用帶有結構的結構對象?

struct structA 
{ 
    byte* pData; int nLength; 

    structA() 
    { 
    pData = NULL; nLength = 0; 
    } 

    structA(int nLen) 
    { 
    pData = new byte[nLen]; nLength = nLen; 
    } 

    ~structA() 
    { 
    delete[] pData; 
    } 
} 

另一種結構即structB具有structA的列表:

struct structB 
{ 
    CList <structA, structA&> AList; 
} 

現在我創建structA的物體參數10作爲輸入,並把它添加到structB的目的:

//的範圍開始

structA osA(10); 
structB osB; 
osB.AList.AddTail(osA); 

//範圍結束

問題是,當程序退出範圍時,structA的結構被調用2次並且程序崩潰。

問題及其原因是什麼? 任何幫助都可以得到

+1

可能的重複http://stackoverflow.com/questions/4172722/what-is-the-rule-of-reeree –

回答

0

您需要複製構造函數(或移動構造函數)。編譯器的默認拷貝構造函數只會複製指針,然後當你退出時,這兩個對象都會刪除相同的指針。

+0

什麼是複製結構? – javad

+0

谷歌「複製構造函數」或閱讀你的教科書(或購買教科書,如果你沒有)。這不是真的適合SO回答的東西。 –

相關問題