Saturday, February 9, 2008

C++: Forward Declarations and Typedef

Consider the situation where I have two header files A.h and B.h

A.h:
class FwdDeclared;

----

B.h:
class Defined {
...
};

typedef Defined FwdDeclared;

----
This is not allowed in C++ [see GOTW: 034]. The GCC compiler complains: "error: Conflicting declaration". I need to look for the rationale for this language behavior. The workaround is to use "typedef Defined FwdDeclared" in A.h too - which means you're not forward declaring the typedefed class at all. I am looking for a better way to do this.

No comments: