2011-10-28 32 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