2011-10-28 31 views
0

我認爲它被稱爲自反聯想,但我不太確定。特殊雙向協會

這是代碼(應該足夠多的,看看有什麼北京時間重要):

CGmae::CGame(void) 
{ 
    CFigure * figure = new CFigure(this); 
} 

CFigure::CFigure(CGame * game) 
{ 
    CGame * game = game; 
} 

我想在類中創建CGame的CFigure的對象,使CFigures知道CGame的和其他方式輪OFC。爲什麼它不適用於'這個'?我需要做些什麼來解決問題?

在此先感謝!

+0

什麼是不工作?什麼是錯誤? – GazTheDestroyer

+0

您是否收到錯誤? – Chowlett

+0

錯字:'CGmae :: CGame'拼寫錯誤。 – Jon

回答

1

工作正常(慣用的改進和拼寫檢查加):

struct CGame; 

struct CFigure 
{ 
    CGame * cg; 
    CFigure(CGame * p) : cg(p) { } 
}; 

struct CGame 
{ 
    CFigure * cf; 
    CGame() : cf(new CFigure(this)) { } 
}; // better be sure to understand memory leaking and exceptions... 

CGame g; // works