Forward declaration inside a class in C ++

I have a question about forward declaration in C ++

class Outer::Inner; // not valid. error
class Outer {
    Inner * inn; // not valid, needs forward declaration.
    class Inner {
    }
}

      

But when implemented like this:

// class Outer::Inner; // not valid. error
class Outer {
    class Inner; // forward-"like" declaration within class
    Inner * inn;
    class Inner {
    }
}

      

Will compile ok. But I have never seen such implementations before (due to my little C ++ experience), so I am curious to see if it will lead to some kind of error or unpredictable behavior in the future.

+3


source to share


1 answer


It's really. The standard says:

9.7 Nested class declarations [class.nest]

If a class is X

defined in the scope of a namespace, a nested class Y

can be declared in the class X

and later defined in the class definition X

(...).

The following example is also provided:



class E {
  class I1; // forward declaration of nested class
  class I2;
  class I1 { }; // definition of nested class
  };
class E::I2 { };

      

Source: C ++ 11 draft n3242

E

and I1

match as Outer

well Inner

in your question.

+2


source







All Articles