Is there a way to predeclare nested classes in C++?

c++, nested, prediction, reference

Solution

In short, the answer is no.

But you should have looked first for a similar question...

EDIT: a possible workaround might be wrapping both classes in another class, and forward delcaring the nested classes inside the wrapper.

class Wrapper
{
public:

   // Forward declarations
   class FirstNested;
   class SecondNested;

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

   // Second class
   class Second
   {
   public:
      FirstNested* fested;
   };
};

This way you'll have to implement `Wrapper::A` and `Wrapper::B` while still isolating them from whatever namespace you're imlpementing.

Problem

Possible Duplicate: Forward declaration of nested types/classes in C++ For simple cross-references of classes it is feasable to predeclare the classnames and use it as reference. In this way the representation is a pointer. But in case I want to cross-reference a nested class of both (look at the example below), I will run into trouble, because there seems to be no way to predeclare a nested class. So my question is: Is there a way to predeclare nested classes so that my example could work? If not: Is there a common workaround for that, that doesn't uglyfy the code too much? ``` // 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 { }; }; ```

Original source

Related problems