[C++] Can I forward declare nested classes?

Started by
10 comments, last by MaulingMonkey 18 years, 10 months ago
I know it's possible with classes within namespaces, but what about classes within classes? Eg.

struct A
{
  struct B
  {
  };
};

how can that be forward declared?
-----------------------------Amma
Advertisement
Maybe its:

struct a::b;

struct A
{
struct B
{
};
};

Just a guess.
IIRC:
class A{   class B;   // A stuff...};class A::B{   // B stuff...};
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Uhhh Dalleboy.. You do know that those aren't forward declarations, right (although doing that is useful to keep clutter down)? At any rate, I'm having this same problem, and I'd love it as well if someone knew how to do this.. And Ace, I've tried that, it didn't work for me.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:Original post by SirLuthor
You do know that those aren't forward declarations, right (although doing that is useful to keep clutter down)?

This code contains the forward declaration of the class A::B. It also defines the class A.
class A{   class B;   // A stuff...};
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
class A::B;

doesn't work in MSVC 6. You get these errors:
error C2653: 'A' : is not a class or namespace nameerror C2079: 'B' uses undefined class 'A'
Quote:Original post by dalleboy
This code contains the forward declaration of the class A::B. It also defines the class A.
*** Source Snippet Removed ***

It may contain the forward declaration of B in A, but that is what I at least wish to avoid, because that requires I include the A header file, or copy it into the file, which is an even worse solution. The point is to be able to forward declare nested classes without defining the holding class as well.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
So nobody knows, or maybe it's actually not possible?
-----------------------------Amma
It is not allowed because the outer class is still incomplete when you would forward declare the nested class. Consider, for example:

/* in first source file */struct A;struct A::B; // not allowed, but let's pretend it isA::B *ptr = 0; // OK, pointer to nested class/* in second source file */struct A {private:    struct B { // Oops, ptr was not OK after all!

Quote:Original post by SirLuthor
Quote:Original post by dalleboy
This code contains the forward declaration of the class A::B. It also defines the class A.
*** Source Snippet Removed ***

It may contain the forward declaration of B in A, but that is what I at least wish to avoid, because that requires I include the A header file, or copy it into the file, which is an even worse solution. The point is to be able to forward declare nested classes without defining the holding class as well.


Are you the alternate account of the OP? I'm a bit confused here.

The OP Stated that they wanted to forward declare A::B, which one can using the method dalleboy posted.

Directly stating:

struct A;
struct A::B;

Is illegal. If you want this kind of behavior, I'd suggest making B a non-nested class... e.g:

struct B; //or B_Impl or whateverstruct A;...struct B {    ...};...struct A {    typedef ::B B;    ...};A::B * ptr = ...;

This topic is closed to new replies.

Advertisement