struct A
{
struct B
{
};
};
[C++] Can I forward declare nested classes?
I know it's possible with classes within namespaces, but what about classes within classes?
Eg.
how can that be forward declared?
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.
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...};
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.
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 SirLuthorQuote: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
Popular Topics
Advertisement