有没有办法在C++中预先声明嵌套类?

Chr*_*ner 5 c++ nested reference prediction

可能重复:
在C++中转发嵌套类型/类的声明

对于类的简单交叉引用,可以预先声明类名并将其用作引用.以这种方式,表示是指针.但是如果我想交叉引用两者的嵌套类(请看下面的例子),我会遇到麻烦,因为似乎无法预先声明嵌套类.

所以我的问题是:有没有办法预先解析嵌套类,以便我的例子可以工作?

如果没有:是否有一个共同的解决方法,那不会过多地丑化代码吗?

// 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 { };
};
Run Code Online (Sandbox Code Playgroud)

Eit*_*n T 4

简而言之,答案是否定的。

但你应该首先寻找类似的问题......

编辑:一种可能的解决方法可能是将这两个类包装在另一个类中,并向前声明包装器内的嵌套类。

class Wrapper
{
public:

   // Forward declarations
   class FirstNested;
   class SecondNested;

   // First class
   class First
   {
   public:
      SecondNested* sested;
   };

   // Second class
   class Second
   {
   public:
      FirstNested* fested;
   };
};
Run Code Online (Sandbox Code Playgroud)

这样,您必须实现它们Wrapper::AWrapper::B同时仍然将它们与您正在实现的任何名称空间隔离。