Why is a forward declaration in a class not a forward declaration to an inner class? C++

Viewed 99

My question is; Why is a forward declaration in a class not a forward declaration to an inner class? Is it defined or undefined? There is no purpose, i only wondered, why it is so.

Example;

class A
{
public:
  class B *b;

  class B
  {
  };
};

class B
{
};

int main()
{
    A a;
    B b;
    A::B ab;

    a.b = &b;  //OK
    a.b = &ab; //Doesn't compile (godbolt.org)

    return 0;
}

Edit: Thanks all. If somebody wonder for namespaces, there is the code.

namespace A
{
  class B *pb;

  /*class B;
  B pb;*/ //It is the same as above. But has another meaning in class

  class B
  {
  };
}

class B
{
};

int main()
{
    B b;
    A::B ab;

    A::pb = &b;  //Never compiles
    A::pb = &ab; //OK

    return 0;
}
0 Answers
Related