4

Is it possible to forward-declare a class declared in another forward-declared class?

Basically, I have something like this

//A.h
class A
{
...
    struct B
    {
    ...
    };
};

and now I want to declare another class like this

//Q.h

class A;
struct A::B;

class Q
{
    A::B* Foo();
};
obamator
  • 478
  • 3
  • 13
  • 1
    Is it that hard to just compile the code above and try it out yourself? – mfontanini Mar 14 '12 at 12:13
  • 1
    @fontanini Ah. But does it work reliably? On all compilers? Is it legal according to the standard? – Konrad Rudolph Mar 14 '12 at 12:15
  • I tried that, naturally. Compiler says "use of undefined type A". – obamator Mar 14 '12 at 12:15
  • 1
    Just by trying to compile it, you can see it does not compile. Therefore: But does it work reliably? No, it does not compile. On all compilers? No, it does not compile. Is it legal according to the standard? No, it does not compile, therefore it can't be legal. – mfontanini Mar 14 '12 at 12:17
  • 6
    @fontanini “It does not compile, therefore it can’t be legal” – bullshit. – Konrad Rudolph Mar 14 '12 at 12:39
  • Ok, replace "can't" with "shouldn't", it's still the same. – mfontanini Mar 14 '12 at 12:41
  • @fontanini: There are many things that should compile and don't, and others that shouldn't compile and do. The result of a particular compiler is no guarantee at all. – David Rodríguez - dribeas Mar 14 '12 at 12:53
  • 1
    @fontanini: others have already commented on the Standard compliance aspect, so I won't go there. I do agree with you that *work reliably* however is very quickly assessed when it fails the test! – Matthieu M. Mar 14 '12 at 13:01

1 Answers1

4

No, it cannot be done. To access the members of A it has to be defined, regardless of whether the member is a type, data or function.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489