You can forward declare classes.
You cannot, however, forward declare an identifier as a type name to be
defined (e.g., by typedef) later.
Why?
If you are in a situation where you write code for a type (not necessarily
just a class) yet unknown, consider writing a template.
Best
Kai-Uwe Bux
I'm using code from a library so I need to create type definitions for
specific types. However, the type definition is defined by using a
templated class that uses it at the same time. Something like this:
typedef Class<MyOwnClass1, MyOwnClass2> ClassA;
// Then, in the definition of MyOwnClass
template <class SomeClass>
class MyOwnClass {
// uses type definition ClassA
someFun(ClassA&);
};
// and then the actual instantiations of MyOwnClass
typedef MyOwnClass<SomeClass1> MyOwnClass1;
typedef MyOwnClass<SomeClass2> MyOwnClass2;
so you can see, that the class MyOwnClass depends on types that are
not yet instantiated.
The way I solved this is by moving the typedefinition of MyOwnClass1
and MyOwnClass2 before the first typedef and using a forward
declaration of the template class MyOwnClass. Very messy eh???? It
turns out that this cyclic dependence is because of the use of a
Visitor class that I need to have for MyOwnClass (visitor design
pattern).
a²