I can declare a pointer to a class that hasn't yet been defined, like this:
class A ;
A* p ;
But how do I do this for a nested class? I want to do this:
class A ;
class A::B ; // error: 'B' in class 'A' does not name a type
A::B* p ;
But it doesn't compile (using g++ 4.5.2). Is there any way to make this work?
There are a number of parts of C++03 that disallow forward declarations of nested classes. In particular, § 7.1.5.3 Elaborated type specifiers:
If an elaborated-type-specifier is the sole constituent of a declaration, the declaration is ill-formed unless it is an explicit specialization (14.7.3), an explicit instantiation (14.7.2) or it has one of the following forms:
class-key identifier ; friend class-key ::optidentifier ; friend class-key ::opttemplate-id ; friend class-key ::optnested-name-specifier identifier ; friend class-key ::optnested-name-specifier templateopttemplate-id ;3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. If the identifier resolves to a class-name or enum-name, the elaborated-type-specifier introduces it into the declaration the same way a simple-type-specifier introduces its type-name. [...] If name lookup does not find a declaration for the name, the elaborated-type-specifier is ill-formed unless it is of the simple form class-key identifier in which case the identifier is declared as described in 3.3.1.
In short, when an identifier is scoped, the compiler must try to resolve the identifier. When the scope is a class, the compiler must look up the declaration for the identifier in the outer class. When the outer class hasn't yet been defined, this can't be done and the result is an ill-formed program.
Consider a namespace instead of nested class.
class A;
A * pa;
namespace A_help
{
class B;
} // namespace A_help
A_help::B * pb;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With