2
內聯函數欲寫像這樣的代碼:一個A
對象調用它的類B
型構件b
的成員函數DoSomething
,並在類B
的DoSomeOtherThing
成員函數,它調用A
的Notify
成員函數。請參見下面的代碼:相互包容在C++
類A:B
class A
{
B b;
public:
A() : b(*this)
{
}
void DoSomething()
{
b.DoSomething();
}
void Notify()
{
}
};
類:
class B
{
A &a;
public:
B(A &a) : a(a)
{
}
void DoSomething()
{
}
void DoSomeOtherThing()
{
a.Notify();
}
};
的問題是我要保持內部函數,所以代碼應該是在頭文件中。我不能簡單地相互包容,因爲它們可能導致相互包容,我不知道在這種情況下是否可以使用前向聲明。我可以在源文件中編寫實現,但它們不會是內聯函數。那麼,我該如何讓代碼工作?
看看:http://stackoverflow.com/questions/23440230/debugging-inheritance-class/23440630#23440630 –