可能重複:
Forward declaration of nested types/classes in C++有沒有辦法在C++中預先聲明嵌套類?
對於類簡單的交叉引用這是feasable到預先聲明的類名,並使用它作爲參考。這樣表示就是一個指針。但是如果我想交叉引用兩者的嵌套類(請看下面的例子),我會遇到麻煩,因爲似乎沒有辦法預先聲明嵌套類。
所以我的問題是:有沒有辦法預先聲明嵌套類,以便我的例子可以工作?
如果沒有:是否有一個共同的解決方法,那不會太醜陋的代碼?
// Need to predeclare it to use it inside 'First'
class Second;
class Second::Nested; // Wrong
// Definition for my 'First' class
class First
{
public:
Second::Nested* sested; // I need to use the nested class of the 'Second' class.
// Therefore I need to predeclare the nested class.
class Nested { };
};
// Definition for my 'Second' class
class Second
{
public:
First::Nested* fested; // I need to use the nested class of the 'First' class.
// This is okay.
class Nested { };
};
好的 - 如果我要查找「聲明」,我會找到這個答案。 (這是不可能的。)就是這樣。謝謝。但是現在我想到一個解決方法。 Unnesting與我的名字空間的另一個'class Nested'類衝突。重命名混淆了界面的用戶。 –