On Nov 19, 10:16 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
aaragon wrote:
Hello, I was wondering if it is possible to forward declare a type
definition.
You can forward declare classes.
You cannot, however, forward declare an identifier as a type name to be
defined (e.g., by typedef) later.
If so, what is the way to do it? I found a couple of
messages where they say it's not possible but there must be a way to
do it.
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, MyOwnClass2ClassA;
// 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<SomeClass1MyOwnClass1;
typedef MyOwnClass<SomeClass2MyOwnClass2;
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²